1

我有一个 UIPanGestureRecognizer。它工作正常。我做了一个 if 语句,所以当有人触摸图片时,它会是 alpha 0.7,并且会大 1.5 倍。alpha 工作正常,但是当我输入 CAAffineTransformMakeScale 方法时,我的图像不会移动。

这是我的代码:

- (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];
   }
}
4

1 回答 1

0

关键问题是您的if陈述没有检查state财产。它应该是:

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

另请注意,此手势识别器仅在您关闭自动布局时才有效。如果您使用自动布局,则必须更改约束。


如果您原谅风格上的观察,我也可能倾向于建议:

  • 使用基于块的动画;

  • 如果不需要,不要引用非局部变量(即引用recognizer.view而不是bloemen),这样可以更轻松地重用此处理程序来拖放UIView您选择添加此手势的各种对象;和

  • 使用标准命名约定,以小写字母开头的方法名称并遵循verbNoun约定。

这些都不是关键的,请根据需要使用或忽略,但这展示了一些最佳实践:

- (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];
    }
}
于 2013-04-23T22:05:41.480 回答