我有这个代码来拖动一个imageView:
in touchbegan:
CGPoint point = [touch locationInView:self];
[imageView setCenter:point];
in touchmoved:
CGPoint point = [touch locationInView:self];
[imageView setCenter:point];
此代码允许在我的手指下方进行imageview并将其移动到其中心。 但我想要的东西允许我不仅从imageview的中心拖动imageview,而且从imageview的另一个点拖动...而不是拖动它,如果我触摸这个imageView ...你能帮助我吗?
答案 0 :(得分:0)
不是仅仅将中心点设置为触摸点,而是需要弄清楚触摸的移动方式。
将触摸点存储在变量中。
在touchesBegan ......
storedPoint = [touchLocationInView:self];
然后在touchesMoved ...
CGPoint currentPoint = [touch locationInView:self];
CGPoint vector = CGPointMake(currentPoint.x - storedPoint.x, currentPoint.y storedPoint.y);
[imageView setCenter:CGPointMake(imageView.center.x + vector.x, imageView.center.y + vecotr.y)];
storedPoint = currentPoint;
无论如何都是这样的。
忽略你可以在touchesBegan中说的触摸......
if (!CGRectContainsPoint(imageView.frame, [touch locationInView:self])
{
//ignore touch
trackTouches = NO;
}
trackTouches = YES;
(trackTouches是一个类var)
然后在touchesMoved中,只有当trackTouches == YES时才会执行操作。
答案 1 :(得分:0)
你需要: A)测试触摸是否在imageView中 B)计算触摸和imageView中心之间的差异。一旦你知道了x / y的差异,就可以做一些简单的数学计算,将中心相对于用户触摸过的imageView上的位置移动。
本网站上有很多关于A的例子,B应该是直截了当的。