2012-07-18 115 views
0

我想滑動UIView同時也旋轉它。最終的結果基本上是一個UIView,它在屏幕上向左旋轉約70度,碰到邊緣,向右旋轉約50度,最後結束靜止(在幾次擺動之後)。我可以讓它滑動和旋轉,但是當我嘗試對第二個旋轉進行動畫處理時(一旦它碰到邊緣),框架會回到原始值(回到屏幕的左側)。一旦UIView滑過,我該如何獲取框架?幻燈片和旋轉UIView

[UIView animateWithDuration:.4 delay:0.0 
        options:UIViewAnimationCurveEaseInOut 
       animations:^{ 
        CGAffineTransform slideAndRotate = CGAffineTransformMakeTranslation(300, 0); 
        slideAndRotate = CGAffineTransformRotate(slideAndRotate, RADIANS(75)); 

        self.ticketImageView.transform = slideAndRotate;     
} completion:^(BOOL finished) { 
    // set the frame to the location where it ended up 
    NSLog(@"%@", NSStringFromCGRect(self.ticketImageView.frame)); 
    [UIView animateWithDuration:.35 
          delay:0.05 
         options:UIViewAnimationCurveEaseInOut 
    | UIViewAnimationOptionBeginFromCurrentState 
        animations:^{ 
         CGAffineTransform rightAngle1 = 
         CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(-45.0)); 
         [[self ticketImageView] setTransform:rightAngle1]; 
    } completion:^(BOOL finished) { 

    }]; 

回答

1

在您的第一個動畫塊中,您正在創建包含平移和旋轉的變換。在第二個動畫塊中,您正在創建包含旋轉但不包含翻譯的變換。因此,當您創建第二個轉換動畫時,您的視圖不再被翻譯。

我看到你已經使用了UIViewAnimationOptionBeginFromCurrentState選項。你可能會認爲這個選項使第一個變換堆棧變成了第二個,但它不是。無論選項如何,第二個轉換都會替換第一個轉換。

A UIView有兩個影響其位置的屬性。一個是transform,你正在使用。另一個是center

通常我們使用center屬性來移動視圖,只使用transform屬性來旋轉和縮放視圖。我建議你也這樣做。

既然你說你要視圖搖晃幾次,我建議你嘗試這樣的事:

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 

    [UIView animateWithDuration:0.4 animations:^{ 
     self.ticketImageView.center = CGPointMake(180, self.ticketImageView.center.y); 
    }]; 

    [self wobbleWithAmount:0.4 direction:1]; 
} 

- (void)wobbleWithAmount:(CGFloat)amount direction:(CGFloat)direction { 
    [UIView animateWithDuration:amount animations:^{ 
     self.ticketImageView.transform = CGAffineTransformMakeRotation(amount * direction * M_PI * 0.5f); 
    } completion:^(BOOL finished) { 
     if (amount > 0.05f) { 
      [self wobbleWithAmount:amount * 0.5f direction:-direction]; 
     } else { 
      [UIView animateWithDuration:amount * 0.5f animations:^{ 
       self.ticketImageView.transform = CGAffineTransformIdentity; 
      }]; 
     } 
    }]; 
} 
+0

感謝您的精彩提示!使用中心肯定比翻譯座標容易得多!這個算法效果很好。 – Clay 2012-07-19 02:21:03

0

當您創建第二變換rightAngle1,你應該用[[self ticketImageView] transform]代替CGAffineTransformIdentity。使用標識轉換,您基本上會擦除第一個轉換並從頭開始。