2

在将 UILabel 的文本设置为手势速度时,我在简单地使用 UIPanGestureRecognizer 翻译视图时遇到了问题。

我已经成功地用这样的平移手势翻译了一个视图(在 IB 的帮助下):

- (IBAction)handleGesture:(UIPanGestureRecognizer *)sender

{   
    CGPoint translation = [sender translationInView:self.view];

    sender.view.center = CGPointMake(sender.view.center.x + translation.x, sender.view.center.y + translation.y);

    [sender setTranslation:CGPointMake(0, 0) inView:self.view];
} 

但是,当我在此方法中添加以下行时,视图会停止翻译,但 UILabel 会随速度更新:

self.myLabel.text = [NSString stringWithFormat:@"%@", [NSNumber numberWithInt:[sender velocityInView:self.roundShape].y]];

我怎样才能解决这个问题?

4

3 回答 3

5

基本上,您从 Pan Gesture Recognizer 获得的翻译值是一个相对值。相对于手势开始识别触摸时的起点。因此,您要做的是保持手势开始的原始点并将平移应用到该点,而不是视图的当前位置。

- (void)gestureDidTranslate:(UIPanGestureRecognizer *)panGesture
{
    if([panGesture state] == UIGestureRecognizerStateBegan){
        self.originalPoint = self.movingView.center;
    }

    CGPoint translation = [panGesture translationInView:self.view];
    self.movingView.center = CGPointMake(self.originalPoint.x+translation.x, self.originalPoint.y+translation.y);
    self.label.text = NSStringFromCGPoint([panGesture velocityInView:self.view]);
}

我想不出你想要在识别器识别时将翻译值归零的时候。

于 2012-12-05T00:00:34.610 回答
0

试试这个。标签更新正确。

@implementation APViewController

- (void)viewDidLoad
{
  [super viewDidLoad];

  UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(Pan:)];
  pan.minimumNumberOfTouches = 1;
  [_View addGestureRecognizer:pan];
}

- (void) Pan:(UIPanGestureRecognizer*)sender
{
  CGPoint translation = [sender translationInView:self.view];
  sender.view.center = CGPointMake(sender.view.center.x + translation.x, sender.view.center.y + translation.y);
  [sender setTranslation:CGPointMake(0, 0) inView:self.view];

  _Label.text = [NSString stringWithFormat:@"%f", [sender velocityInView:self.view].y];
}

@end

如果您AUTOLAYOUT的 XIB 上有一个,请取消选择它,因为视图不会移动。为什么?我不知道。

希望这可以帮助。

于 2012-12-02T19:41:55.590 回答
0

使用自动布局!

这一切都可以使用 Interface Builder。这是我用来执行此操作的代码:

@property (weak,nonatomic) IBOutlet NSLayoutConstraint *buttonXConstraint;
@property (weak,nonatomic) IBOutlet NSLayoutConstraint *buttonYConstraint;

然后将这些 IBOutlets 连接到 Interface Builder 中的水平约束(X 位置)和垂直约束(Y 位置)。确保约束连接到基本视图,而不是最近的视图。

连接一个平移手势,然后使用以下代码拖动您的对象:

- (IBAction)panPlayButton:(UIPanGestureRecognizer *)sender
{
    if(sender.state == UIGestureRecognizerStateBegan){

    } else if(sender.state == UIGestureRecognizerStateChanged){
        CGPoint translation = [sender translationInView:self.view];

        //Update the constraint's constant
        self.buttonXConstraint.constant += translation.x;
        self.buttonYConstraint.constant += translation.y;

        // Assign the frame's position only for checking it's fully on the screen
        CGRect recognizerFrame = sender.view.frame;
        recognizerFrame.origin.x = self.buttonXConstraint.constant;
        recognizerFrame.origin.y = self.buttonYConstraint.constant;

        // Check if UIImageView is completely inside its superView
        if(!CGRectContainsRect(self.view.bounds, recognizerFrame)) {
            if (self.buttonYConstraint.constant < CGRectGetMinY(self.view.bounds)) {
                self.buttonYConstraint.constant = 0;
            } else if (self.buttonYConstraint.constant + CGRectGetHeight(recognizerFrame) > CGRectGetHeight(self.view.bounds)) {
                self.buttonYConstraint.constant = CGRectGetHeight(self.view.bounds) - CGRectGetHeight(recognizerFrame);
            }

            if (self.buttonXConstraint.constant < CGRectGetMinX(self.view.bounds)) {
                self.buttonXConstraint.constant = 0;
            } else if (self.buttonXConstraint.constant + CGRectGetWidth(recognizerFrame) > CGRectGetWidth(self.view.bounds)) {
                self.buttonXConstraint.constant = CGRectGetWidth(self.view.bounds) - CGRectGetWidth(recognizerFrame);
            }
        }

        //Layout the View
        [self.view layoutIfNeeded];
    } else if(sender.state == UIGestureRecognizerStateEnded){

    }

    [sender setTranslation:CGPointMake(0, 0) inView:self.view];
}

我在那里添加了代码来检查框架并确保它不会超出视图。如果您想让对象部分离开屏幕,请随意将其取出。

我花了一点时间才意识到是使用 AutoLayout 来重置视图,但约束会在这里做你需要的!

于 2014-03-19T03:56:56.040 回答