xcode,ios,帮助图像冻结,不移动

时间:2015-09-04 20:24:30

标签: ios xcode

我正在尝试将图像移动到逐像素触摸的位置。 但当我运行这个并按某处图像移动到屏幕的右侧并且之后不移动。

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];

}

1 个答案:

答案 0 :(得分:0)

手势识别器以这种方式工作: UIGestureRecognizerStates

因此,在您的代码移动中:当用户点击时,将调用一次。

创建一个属性NSTimer * timer并在touchBegan里面添加以下代码:

self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(move:) userInfo:nil repeats:TRUE];

在touchEnded和touchCancelled内部添加[self.timer invalidate];以在触摸完成时停止计时器。