【问题标题】:UIPanGestureRecognizer problem in iPadiPad 中的 UIPanGestureRecognizer 问题
【发布时间】:2011-08-13 09:26:08
【问题描述】:

在我的应用程序中,我正在尝试移动弹出的子视图。这是使用苹果提供的 UIPanGestureRecognizer 和手势识别功能in this link

所以我遇到的问题是,当我单击按钮图像并尝试移动视图时,它不会移动视图。只有当我单击按钮,然后单击并移动它时,该功能才有效。只有这样它才会移动视图。

我想知道我做错了什么。

这是我添加此功能的按钮代码

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;
}

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

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

【问题讨论】:

    标签: xcode ios4 ipad uigesturerecognizer pan


    【解决方案1】:

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

    这是更新后的代码...

    图像的代码 -

    // 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];
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-19
      • 1970-01-01
      相关资源
      最近更新 更多