-1

Hi guys I am preety new here.
And I have a UIimageview.
And I know what is the coordinates of a UITouch.
Now I want the ball(UIImageView) to go towards it.
I tried to think of ways to do this...
Using CGPoint
But I cant think on any way that will work properly
How can I do that? (i didnt found answer in the internet)

    - (void)viewDidLoad
{
    [super viewDidLoad];
    [self play];
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(play) userInfo:nil repeats:YES];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    ballvelocity = [touch locationInView:self.view];

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

    UITouch *touch = [touches anyObject];
    ballvelocity = [touch locationInView:self.view];

}

-(void) play{
    [UIView animateWithDuration:3 animations:^{
        ball.center = CGPointMake(ballvelocity.x, ballvelocity.y);
    }];
}
  • Please post what you've tried. It's expected on Stack Overflow that you will show some effort and we will help you from there. – jscs Mar 04 '12 at 18:41

1 Answers1

1

if you are good with block..

you can create a animation with

UIView animateWithDuration:(NSTimeInterval) animations:^(void)>

other wise

UIView beginAnimations:(NSString *) context:(void *)

in both the animation methods you can set

[imageView setCenter:yourlocation];

this will animate it to the position.

more info on animating views here : https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816

Shubhank
  • 21,721
  • 8
  • 65
  • 83
  • hmm will it work if the position of the touch is always changing? – user1232989 Mar 04 '12 at 15:22
  • no problem..google UIView beginAnimations tutorials...i will be able to solve your problems...but putting a full fledged tutorials here. which are widely available on internet is silly..dont worry about the block one..you will learn its advantage eventually..dont focus on it now, – Shubhank Mar 04 '12 at 15:24
  • 1
    @user1232989: if you want to be able to drag the view with your finger then check both answers at http://stackoverflow.com/questions/4982277/uiview-drag-image-and-text/8332581#8332581 – Rok Jarc Mar 04 '12 at 15:26
  • yeah..a new lesson to you..alwasys try to search your problem on google and stack overflow first.. – Shubhank Mar 04 '12 at 15:28
  • thank you very much for your answer! im checking it and will tell you if it works thank you very much :) – user1232989 Mar 04 '12 at 15:39
  • ok I looked in the web and did this:[UIView beginAnimation:nil context:nil]; [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; [UIView setAnimationDuration:3]; [ball setCenter:CGPointMake(ballvelocity.x, ballvelocity.y)]; [UIView commitAnumations]; and the ball just jump to the position of the touch i want it to "walk" to it – user1232989 Mar 04 '12 at 16:04
  • by the way i've putted that^^^^ inside of -(void)touchesMoved – user1232989 Mar 04 '12 at 16:18
  • your uiview animation code is getting called repeatedly..in touches moved..i will suggest making a counter to make the animation called only once ...and reset the counter...basically you can set a delegate method of the animation..which is called after the animation is completed...reset the counter there..search for animation delgae.. – Shubhank Mar 04 '12 at 16:29
  • i have set a timer for that... its not updating this: [ball setCenter:CGPointMake(ballvelocity.x, ballvelocity.y)]; i updated the x and y of the ball velocity while the animation is working and its not updating in the animation what can i do? – user1232989 Mar 04 '12 at 16:44
  • try [UIView setAnimationDuration:3.0] .. what you are experiencing shouldn't really be there...update the question with your code.. – Shubhank Mar 04 '12 at 16:54
  • - (void)viewDidLoad { [super viewDidLoad]; [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(play) userInfo:nil repeats:YES]; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; ballvelocity = [touch locationInView:self.view]; } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; ballvelocity = [touch locationInView:self.view]; } -(void) play{ [UIView animateWithDuration:3 animations:^{ ball.center = CGPointMake(ballvelocity.x, ballvelocity.y); – user1232989 Mar 04 '12 at 19:47
  • sorry for the mess^ hope you understand something – user1232989 Mar 04 '12 at 19:48
  • some simple debugging steps..change duration to 3.0 , make the timer autorepeat to NO ... try to amok a simple new project and try the animation with some random values..like set center:CGPointMake(160,240) .. rather than your touch.. you will see different results and then you will get the idea what might be a problem – Shubhank Mar 05 '12 at 07:31