我的超级视图里面有一个子视图。在子视图中,我有另一个名为circle的子视图。
这个圆圈是一个可拖动的圆圈。我可以通过触摸圆圈并移动来拖动圆圈。问题是,如果我触摸子视图上的任何其他位置并移动,则圆圈会移动。如何防止这种情况发生。
以下代码放在子视图的类中,用于移动圆圈。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
startPoint = [[touches anyObject] locationInView:circle];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches){
CGPoint newPoint = [touch locationInView:self];
newPoint.x -= startPoint.x;
newPoint.y -= startPoint.y;
CGRect frm = [circle frame];
frm.origin = newPoint;
[circle setFrame:frm];
}
}
答案 0 :(得分:0)
尝试忽略圆圈视图中没有的触摸?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch1 = [touches anyObject];
if(touch1.view == circle)
{
startPoint = [[touches anyObject] locationInView:self];
}
else
{
startPoint = nil;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (!startPoint) //If touch didnt start on circle view then ignore it
{
return;
}
for (UITouch *touch in touches){
CGPoint newPoint = [touch locationInView:self];
newPoint.x -= startPoint.x;
newPoint.y -= startPoint.y;
CGRect frm = [circle frame];
frm.origin = newPoint;
[circle setFrame:frm];
}
}