私のアプリでは、ポップアップするサブビューを移動しようとしています。これは、このリンクでAppleが提供する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;
}
誰かがこれで私を助けることができれば、それは本当に素晴らしいことです.
更新: 問題は解決しました。以下の回答を見てください。