nahid
nahid

Reputation: 407

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. 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

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.

Upvotes: 1

Views: 4580

Answers (5)

Leon
Leon

Reputation: 410

Actually Apple made a demo for this.

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 :-).

Upvotes: 1

Dipen Panchasara
Dipen Panchasara

Reputation: 13600

Step 1: Put this code in your ViewDidLoad Method, in which i have created some UIImageView and add it to View Randomly

[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

- (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)];
        }];
    }
}

Upvotes: 2

Mev
Mev

Reputation: 1625

  • (void)touchesBegan:(NSSet *)touches 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.

.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.

In your mainview just make an instance of StarView & add to mainview

Upvotes: 0

ViggoV
ViggoV

Reputation: 2173

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..

  1. Create a public method in your ViewController with return type IBAction
  2. Create a button in IB and connect its "Touch Up Inside" outlet to your IBAction
  3. I your IBAction method, generate a random x and y coordinate within the screens bounds and animate the movement to this point.

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. Also, you might wan't to save stuff like animations until you are a bit more experienced with iOS and Objective-C

-V

Upvotes: 1

kmikael
kmikael

Reputation: 5022

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:
    }
}

Upvotes: 1

Related Questions