0
在我的應用程序,我想移動一個子視圖,我彈出。這是使用UIPanGestureRecognizer和手勢識別功能提供的蘋果in this linkUIPanGestureRecognizer在iPad中的問題
所以我得到的問題是,當我點擊按鈕圖像,並嘗試移動視圖,它不會移動視圖。該功能僅在單擊按鈕時起作用,然後單擊並移動它。只有這樣它才能移動視圖。
我想知道我在做什麼錯。
這裏是按鈕的代碼,我添加此功能
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;
}
這將是,如果真的很棒有人可以幫我解決這個問題。
更新:解決問題。看看下面提供的答案。