在我的应用程序中,我试图移动我弹出的子视图。这是正在使用苹果在此链接中提供的 UIPanGestureRecognizer 和手势识别功能
所以我遇到的问题是,当我单击按钮图像并尝试移动视图时,它不会移动视图。只有当我单击按钮,然后单击并移动它时,该功能才有效。只有这样它才会移动视图。
我想知道我做错了什么。
这是我添加此功能的按钮代码
UIButton *moveButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 1, 30, 30)];
[moveButton addTarget:self action:@selector(moveButtonClick:)forControlEvents:UIControlEventTouchDown];
[moveButton setBackgroundImage:[UIImage imageNamed: @"moveButton.png"] forState:UIControlStateNormal];
[customView addSubview:moveButton];
[moveButton release];
这是我用于应用程序识别平移手势的代码
-(void) moveButtonClick: (id) sender
{
[self addGestureRecognizersToPiece:self.view];
}
// shift the piece's center by the pan amount
// reset the gesture recognizer's translation to {0, 0} after applying so the next callback is a delta from the current position
- (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer
{
UIView *piece = [gestureRecognizer view];
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGPoint translation = [gestureRecognizer translationInView:[piece superview]];
[piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y + translation.y)];
[gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
}
}
// adds a set of gesture recognizers to one of our piece subviews
- (void)addGestureRecognizersToPiece:(UIView *)piece
{
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panPiece:)];
[panGesture setDelegate:self];
[panGesture setMaximumNumberOfTouches:1];
[piece addGestureRecognizer:panGesture];
[panGesture release];
}
// scale and rotation transforms are applied relative to the layer's anchor point
// this method moves a gesture recognizer's view's anchor point between the user's fingers
- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
{
UIView *piece = gestureRecognizer.view;
CGPoint locationInView = [gestureRecognizer locationInView:piece];
CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview];
piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
piece.center = locationInSuperview;
}
}
// UIMenuController requires that we can become first responder or it won't display
- (BOOL)canBecomeFirstResponder
{
return YES;
}
如果有人能在这方面帮助我,那就太好了。
更新:问题已解决。看看下面提供的答案。