0

在我的应用程序中,我试图移动我弹出的子视图。这是正在使用苹果在此链接中提供的 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;
}

如果有人能在这方面帮助我,那就太好了。

更新:问题已解决。看看下面提供的答案。

4

1 回答 1

0

我解决了这个问题......显然使用按钮来执行我需要的操作,确实需要我点击它两次,然后只执行平移操作。所以我删除了它并添加了一个图像并添加了手势识别器。

这是更新的代码...

图片的代码 -

// Image which allows subview to be moved around
CGRect myImageRect = CGRectMake(5, 1, 30, 30);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"moveButton.png"]];
myImage.opaque = YES;
myImage.userInteractionEnabled = YES;
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self        action:@selector(panPiece:)];
[panGesture setDelegate:self];
[panGesture setMaximumNumberOfTouches:1];
UIView *piece = self.view;
[piece addGestureRecognizer:panGesture];
[panGesture release];
[customView addSubview:myImage];
[myImage release];

在此之后,我删除了按钮代码并删除了按钮的动作识别器..除此之外,其余代码是相同的,它工作正常......

于 2011-04-28T15:19:07.240 回答