我有3 UIViews
:needToDragView
,superView
和self.view
。当我使用touchesMoved:
仅拖动needToDragView
时(3视图中最低)。但它可以拖动所有3View。如何检测最低的UIView?
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UIView *dragView = [[touches anyObject] view];
CGPoint newCenter = [[touches anyObject] locationInView:dragView.superview];
dragView.center = newCenter;
}
答案 0 :(得分:3)
试试这个:
确保为needToDragView设置UserInteractionEnabled = YES;
。
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
{
UITouch *touch = [touches anyObject];
if ([touch view]== needToDragView) {
// Drag Image Here
}
else {
// NSLog("Wrong");
}
}
<强>更新强>
除此之外,你还可以像这样使用UIPanGestureRecognizer
:
UIPanGestureRecognizer pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(function:)];
[pan setMinimumNumberOfTouches:1];
[pan setMaximumNumberOfTouches:2];
[needsToDragView addGestureRecognizer:pan];
[needsToDragView setUserInteractionEnabled:YES];
- (void)function:(UIPanGestureRecognizer *)recognizer
{
NSLog(@"Moving View");
mainView= recognizer.view;
CGPoint translatedPoint = [recognizer translationInView:self.view];
if([recognizer state] == UIGestureRecognizerStateBegan)
{
firstX = [mainView center].x;
firstY = [mainView center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[mainView setCenter:translatedPoint];
}