仅在触摸图像时拖动图像

时间:2014-04-01 19:14:07

标签: ios xcode5

我有一个图像,我在屏幕上拖动但是,无论我在哪里触摸屏幕,图像都会出现在我的手指上并开始拖动,我观看的所有视频以及人们正在展示的所有博客和文章都是该怎么做...它不是我想要的...我想要的只是当我触摸图像时开始拖动图像..继承人我的代码

- (void)touchesMoved:(NSSet *)触及withEvent:(UIEvent *)事件

{

UITouch *Drag = [ [  event allTouches ] anyObject ];
Dot.center = [ Drag locationInView: self.view ];

[self checkCollison];

}

谢谢

2 个答案:

答案 0 :(得分:0)

在你的.h

IBOutlet UIImageView * YOURIMAGE;

在你的.m

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    if ([touch view] == YOURIMAGE) {
        YOURIMAGE.center = [touch locationInView:self.view];
    }
}

答案 1 :(得分:0)

而不是使用-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event方法使用UIPanGestureRecognizer

为您的UIImageView创建并添加平移手势,例如:

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleDrag:)];
[yourImageView addGestureRecognizer:panGesture];

并编写选择器方法,如:

- (void)handleDrag:(UIPanGestureRecognizer*)recognizer
{

    CGPoint translation = [recognizer translationInView:recognizer.view];
    CGPoint velocity    = [recognizer velocityInView:recognizer.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                     recognizer.view.center.y + translation.y);

    if (recognizer.state == UIGestureRecognizerStateBegan)
    {
       // start tracking movement
    }
    else if (recognizer.state == UIGestureRecognizerStateEnded)
    {
         // You can change position here, with animation
    }
 }