简体   繁体   English

如何通过在iOS中触摸对象来使其移动

[英]how to make an object move by touching it in iOS

I am a newbie in Objective-C and trying to make a simple app in which when you touch the object it will move randomly for a while and stops. 我是Objective-C的新手,正在尝试制作一个简单的应用程序,在该应用程序中,当您触摸对象时,它会随机移动一段时间并停止。 then you need to touch it again so it will move again and stop after a while. 那么您需要再次触摸它,使其再次移动并过一会儿停止。

I have searched for touch method and some tutorials, but the problem is I don't know how to start. 我已经搜索了触摸方法和一些教程,但是问题是我不知道如何开始。 It is like I need a function to move the object and one to touch it, but I don't know how to connect them and use them. 就像我需要一个移动对象并触摸它的功能,但是我不知道如何连接和使用它们。

here is a tutorial which helped me a lot to get a view of functionality and it actually function in opposite way of my app. 这是一个教程,它对我的​​功能很有帮助,并且实际上以与我的应用相反的方式起作用。 but still I can not start programming on my own. 但是我仍然不能自己开始编程。

http://xcodenoobies.blogspot.se/2010/11/under-construction.html http://xcodenoobies.blogspot.se/2010/11/under-construction.html

Any help would be appreciated, regarding how to program my logic and how to find the right methods and how to find the type of variables I need. 对于如何编程逻辑,如何找到正确的方法以及如何找到所需变量的类型,您将获得任何帮助。

Step 1: Put this code in your ViewDidLoad Method, in which i have created some UIImageView and add it to View Randomly 第1步:将此代码放入您的ViewDidLoad方法中,在其中我创建了一些UIImageView并将其随机添加到View中

[self.view setTag:1];
for(int i=0;i<4;i++)
{
    int x = arc4random()%300;
    int y = arc4random()%400;

#warning set Image here

    UIImageView *imgview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"someImage.png"]];
    [imgview setFrame:CGRectMake(x, y, 25, 25)];
    [imgview setUserInteractionEnabled:YES];
    [self.view addSubview:imgview];
}

Step 2 : Define touchBegan Method to handle touch and move objects around the view, we have set Tag = 1 for ViewController ,because we dont want to move our mainview, only subviews will be moved 第2步:定义touchBegan方法来处理触摸并在视图中移动对象,我们为ViewController设置了Tag = 1,因为我们不想移动主视图,因此只会移动子视图

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    if([[touch view] tag] != 1)
    {
        [UIView animateWithDuration:0.25f animations:^{
            int x = arc4random()%300;
            int y = arc4random()%400;

            [[touch view] setCenter:CGPointMake(x, y)];
        }];
    }
}

What you need is to add a gesture recognizer to the view you want to be able to touch: 您需要在想要触摸的视图中添加一个手势识别器:

// In a view controller or in a UIView subclass
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[self addGestureRecognizer:tapGestureRecognizer];
// Or [self.view addGestureRecognizer:tapGestureRecognizer];

- (void)handleTap:(UITapGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateEnded) {
        // Animate the view, move it around for a while etc.
        // For animating a view use animateWithDuration:animations:
    }
}

Actually Apple made a demo for this. 实际上,苹果为此做了一个演示。

http://developer.apple.com/library/ios/#samplecode/MoveMe/Introduction/Intro.html http://developer.apple.com/library/ios/#samplecode/MoveMe/Introduction/Intro.html

You can try to modify this code to your needs. 您可以尝试根据需要修改此代码。 And the actual functions you where looking for where: 而实际的功能你在哪里寻找哪里:

- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event 
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event 

If this is the answer you where looking for please click "answered" so this question can be considered as closed :-). 如果这是您要查找的答案,请单击“已回答”,这样该问题可以视为封闭的:-)。

If I get you correctly I would say that the easiest way to achieve what you request is to create a UIButton in Interface Builder and connect it to an IBAction which moves it to a random spot.. You can then add a custom graphic to the button.. 如果我正确理解了,我会说,实现您所要求的最简单的方法是在Interface Builder中创建一个UIButton并将其连接到IBAction,该IBAction将其移动到随机位置。然后,您可以向按钮添加自定义图形..

  1. Create a public method in your ViewController with return type IBAction 在ViewController中使用返回类型IBAction创建公共方法
  2. Create a button in IB and connect its "Touch Up Inside" outlet to your IBAction 在IB中创建一个按钮并将其“ Touch Up Inside”插座连接到IBAction
  3. I your IBAction method, generate a random x and y coordinate within the screens bounds and animate the movement to this point. 在您的IBAction方法中,在屏幕范围内生成一个随机的x和y坐标,并将运动设置为动画。

I will/can not go into details on the specific code since it would take way to much space. 我将/无法详细介绍特定代码,因为这将占用很多空间。 Note that your question is very open and vague which is not considered good style on StackOverflow. 请注意,您的问题是非常开放和模糊的,在StackOverflow上,这不是好样式。 Also, you might wan't to save stuff like animations until you are a bit more experienced with iOS and Objective-C 另外,除非您对iOS和Objective-C有所了解,否则您可能不希望保存动画等内容。

-V -V

  • (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; (void)touchesBegan:(NSSet *)touch withEvent:(UIEvent *)event;

is a method called when user touches the view. 是当用户触摸视图时调用的方法。 if you override this in mainview (big view) you will have to find if touched point is where the object is with some helper method as described in your link. 如果您在主视图(大视图)中覆盖此位置,则必须使用链接中所述的一些辅助方法来确定接触点是否是对象所在的位置。 else you can override your object's class & implement the method so you dont have to explicitly find if touched point is on the object or not. 否则,您可以覆盖对象的类并实现该方法,因此您不必显式地找到接触点是否在对象上。

for your requirement. 根据您的要求。 i'd say override the uiimageview & inside that put the touchesbegan implementation it will just work fine. 我会说重写uiimageview&里面的touchesbegan实现,它将正常工作。

.h file 

@interface StarView : UIImageView

@end

.m file 

@implementation StarView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // your code in the link with the related methods

Destination = CGPointMake(arc4random() % 320, arc4random() % 480);   

// calculate steps based on speed specified and distance between the current location of the sprite and the new destination  

xamt = ((Destination.x - self.center.x) / speed);  

yamt = ((Destination.y - self.center.y) / speed);  

// ask timer to execute moveBall repeatedly each 0.2 seconds. Execute the timer.   

mainTimer = [NSTimer scheduledTimerWithTimeInterval:(0.02) target:self selector:@selector(moveBall) userInfo:nil repeats: NO]; 
}

dont forget to copy the moveBall method after this. 之后不要忘记复制moveBall方法。

In your mainview just make an instance of StarView & add to mainview 在您的主视图中,只需创建一个StarView实例并添加到主视图中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM