2012-03-04 55 views
-1

嗨,大家好我在这里很好。
而我有一个UIimageview
我知道UITouch的坐标是什么。
现在我想要球(UIImageView)朝它走。
我试图想办法做到这一点...
使用CGPoint
但我想不出任何办法,将工作正常
我怎样才能做到这一点? (我没有在互联网上找到答案)Xcode对象移动到手指即时新这里

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

请发表你试过什么。 Stack Overflow预计你会付出一些努力,我们会从那里帮助你。 – 2012-03-04 18:41:48

回答

1

如果你善于与块..

,你可以创建一个动画

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

其他明智

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

在这两种动画方法中你都可以设置

[imageView setCenter:yourlocation]; 

这会将其移动到该位置。在这里动画的看法

更多信息: https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816

+0

如果触摸的位置总是在变化,它会起作用吗? – user1232989 2012-03-04 15:22:02

+0

没问题..google UIView beginAnimations教程...我将能够解决您的问题......但在这里放置一个完整的教程。这是在互联网上广泛可用是愚蠢的..不担心块一..你会最终学习它的优势..现在专注于它, – Shubhank 2012-03-04 15:24:51

+1

@ user1232989:如果你想能够用你的手指拖动视图,然后检查两个答案在http://stackoverflow.com/questions/4982277/uiview-drag-image-and-text/8332581#8332581 – 2012-03-04 15:26:08