我正在尝试将图像移动到逐像素触摸的位置。 但当我运行这个并按某处图像移动到屏幕的右侧并且之后不移动。
invisibleimage是我触摸的标记,imageView是图像。
我知道有一种方法可以做到这一点因为我在一个名为Club Penguin的游戏中看过它。 谢谢你的帮助!
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event touchesForView:self.view] anyObject];
wheretouch1 = [touch locationInView:self.view];
if (imageView != nil) {
[imageView removeFromSuperview];
}
[self.view addSubview:imageView];
invisibleimage.center = wheretouch1;
[self performSelector:@selector(move:) withObject:nil afterDelay:0.1];
}
-(void)move:(id)sender{
if (CGPointEqualToPoint(imageView.center, invisibleimage.center)) {
NSLog(@"At Destination");
}
if (imageView.center.x > invisibleimage.center.x){
imageView.center = CGPointMake(imageView.center.x - 1, imageView.center.y);
[self performSelector:@selector(move:) withObject:nil afterDelay:0.5];
}
else if (imageView.center.x < invisibleimage.center.x){
imageView.center = CGPointMake(imageView.center.x + 1, imageView.center.y);
[self performSelector:@selector(move:) withObject:nil afterDelay:0.5];
}
if (imageView.center.y > invisibleimage.center.y) {
imageView.center = CGPointMake(imageView.center.y - 1, imageView.center.y);
[self performSelector:@selector(move:) withObject:nil afterDelay:0.5];
}
else if (imageView.center.y < invisibleimage.center.y){
imageView.center = CGPointMake(imageView.center.y + 1, imageView.center.y);
[self performSelector:@selector(move:) withObject:nil afterDelay:0.5];
}