2015-06-07 215 views
0

我試圖從屏幕上將圖像從左側滑動到屏幕上,並停止在視圖中的中心點。如果可能的話,我想保留它在y位置(IE圖像設置在故事板中)。iOS如何從屏幕上啓動圖像並將其動畫到屏幕上

我正在閱讀本教程,但它在swift中,我無法在objective-c中進行相同的分配。

http://www.raywenderlich.com/95910/uiview-animation-swift-tutorial

它說來襯托畫面(SWIFT)的觀點:

heading.center.x -= view.bounds.width 

然後動畫(斯威夫特):

UIView.animateWithDuration(0.5, animations: { 
    self.heading.center.x += self.view.bounds.width 
}) 

但是我不能操控中心。 x在目標c中像他們正在迅速做的那樣。

我已經嘗試調整viewWillAppear中的圖像的初始位置通過更改界限,沒有運氣。

編輯:

這裏是我試圖把它定位關閉屏幕:

self.heading.center = CGPointMake(-200, self.heading.center.y); 

不管我怎麼負設置視圖仍然會出現在屏幕上的x位置。實際上不管我設置的x位置到視圖中都不會在x方向上移動。我也嘗試設置框架

回答

1

x視圖中心的座標不能直接在Objective-C中指定。相反,嘗試設置整個中心點。在你的榜樣,這可能是這個樣子:

[UIView animateWithDuration:0.5 animations:^{ 
    CGFloat newCenterX = self.heading.center.x + self.view.bounds.size.width; 
    self.heading.center = CGPointMake(newCenterX, self.heading.center.y); 
}]; 
+0

沒有解釋如何得到它最初關閉屏幕。看我的編輯。 – lostintranslation

+1

@lostintranslation所以你不能以編程方式影響視圖的位置?您是否在故事板或其他內容中啓用了[自動佈局](http://i.imgur.com/sNQ99rK.png)的超級視圖?如果是這種情況,則適用於視圖的任何自動佈局約束都將阻止您自己操縱其框架。 – Gamma

+0

好吧,那就是它。很確定我有自動佈局和約束。雖然這引發了關於如何對齊和約束跨不同設備和風景/肖像的視圖的各種其他問題。所以沒有辦法使用動畫,並有約束? BTW感謝您的幫助,真正感激。 – lostintranslation

0

嘗試使用CGAffineTransformMakeTranslation:

 [UIView animateKeyframesWithDuration:5 
           delay:0 
           options:UIViewKeyframeAnimationOptionCalculationModeLinear 
           animations:^{ 

           view.transform = CGAffineTransformMakeTranslation(550, -40); 
           }]; 
相關問題