简体   繁体   English

我想调整PanGestureRecognizer的大小时不会移动

[英]My PanGestureRecognizer won't move when i want to resize it

I Have a UIPanGestureRecognizer. 我有一个UIPanGestureRecognizer。 It works fine. 工作正常。 I made an if statement so when someone touches the picture, it will be alpha 0.7 and it will be 1,5 times bigger. 我做了一个if语句,所以当有人触摸图片时,它将为alpha 0.7,并且将是其1.5倍。 The alpha works fine, but when i type in the CAAffineTransformMakeScale method, my image won't move. Alpha工作正常,但是当我输入CAAffineTransformMakeScale方法时,我的图像将不会移动。

this is my code: 这是我的代码:

- (IBAction)Bloemen:(UIPanGestureRecognizer *)recognizer {

    CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                     recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

     if (UIGestureRecognizerStateBegan)
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDelay:0.1];
        [UIView setAnimationDuration:0.4];

        bloemen.alpha = 0.7f;
        bloemen.transform = CGAffineTransformMakeScale(1.5,1.5);

        [UIView commitAnimations];
     }
    if (UIGestureRecognizerStateEnded) {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDelay:0.1];
        [UIView setAnimationDuration:0.1];

        bloemen.alpha = 1.0f;
        bloemen.transform = CGAffineTransformIdentity;

       [UIView commitAnimations];
   }
}

The critical issue is that your if statements are not checking the state property. 关键问题是您的if语句不检查state属性。 It should be: 它应该是:

if (recognizer.state == UIGestureRecognizerStateBegan)
{
    // began code here
}
else if (recognizer.state == UIGestureRecognizerStateEnded)
{
    // ended code here
}

Also be aware that this gesture recognizer will only work if you have autolayout turned off. 另请注意,此手势识别器仅在您关闭自动版式时才起作用。 If you're using autolayout, you have to change the constraints. 如果使用自动布局,则必须更改约束。


If you forgive the stylistic observations, I might also be inclined to suggest: 如果您原谅风格方面的观察,我可能还会建议:

  • use block-based animations; 使用基于块的动画;

  • not reference non-local variables if not needed (ie reference recognizer.view rather than bloemen ), which makes it easier to reuse this handler for dragging and dropping of various UIView objects you choose to add this gesture to; 不能引用非局部变量,如果没有必要(即参考recognizer.view而非bloemen ),这使得它更易于重复使用此处理拖动和各种掉落UIView对象,您选择添加这种姿态; and

  • use standard naming conventions, start method names with lowercase letter and follow the verbNoun convention. 使用标准命名约定,以小写字母开头的方法名称,并遵循verbNoun约定。

None of this is critical and please use or disregard as you see fit, but this exhibits a few best practices: 这些都不是至关重要的,请根据自己的喜好使用或忽略,但这体现了一些最佳做法:

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

    if (recognizer.state == UIGestureRecognizerStateBegan)
    {
        [UIView animateWithDuration:0.4
                              delay:0.0  // you had 0.1, but seems worthwhile to give immediate feedback
                            options:0
                         animations:^{
                             recognizer.view.alpha = 0.7f;
                             recognizer.view.transform = CGAffineTransformMakeScale(1.5,1.5);
                         }
                         completion:nil];
    }
    if (recognizer.state == UIGestureRecognizerStateEnded)
    {
        [UIView animateWithDuration:0.1
                              delay:0.0  // you had 0.1, but seems worthwhile to give immediate feedback
                            options:0
                         animations:^{
                             recognizer.view.alpha = 1.0f;
                             recognizer.view.transform = CGAffineTransformIdentity;
                         }
                         completion:nil];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM