在设置UILabel文本时使用UIPanGestureRecognizer进行翻译

时间:2012-11-07 11:50:50

标签: objective-c uilabel uipangesturerecognizer

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

我成功地用这样的平移手势翻译视图(在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]];

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:4)

基本上,您从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]);
}

我想不出一个时间,想要将识别器识别时的翻译值清零。

答案 1 :(得分: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

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

希望这会有所帮助。

答案 2 :(得分:0)

使用Autolayout!

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

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

然后将这些IBOutlet连接到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正在重置视图,但约束会在这里做你需要的!