Ios 仅在触摸图像时拖动图像
我有一个图像,我在我的屏幕上拖来拖去,但无论我触摸屏幕的什么地方,图像都会出现在我的手指上并开始拖动,对于我观看的所有视频以及所有博客和文章,人们展示的都是如何做到这一点。。。这不是我想要的。。我想要的是仅当我触摸图像时才开始拖动图像。。这是我的密码 -(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件 { } 谢谢你的.hIos 仅在触摸图像时拖动图像,ios,xcode5,Ios,Xcode5,我有一个图像,我在我的屏幕上拖来拖去,但无论我触摸屏幕的什么地方,图像都会出现在我的手指上并开始拖动,对于我观看的所有视频以及所有博客和文章,人们展示的都是如何做到这一点。。。这不是我想要的。。我想要的是仅当我触摸图像时才开始拖动图像。。这是我的密码 -(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件 { } 谢谢你的.h IBOutlet UIImageView * YOURIMAGE; 在你的房间里 - (void)touchesMoved:(NSSet *)touch
IBOutlet UIImageView * YOURIMAGE;
在你的房间里
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([touch view] == YOURIMAGE) {
YOURIMAGE.center = [touch locationInView:self.view];
}
}
不要使用-(void)touchemoved:(NSSet*)toucheevent:(UIEvent*)event
方法使用
创建平移手势并将其添加到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
}
}
使用UIPANGESTURE来代替它,我喜欢它有多简单,但它不起作用。。如果我把你的代码放在点上,它就不会动了。。。
- (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
}
}