将UIView从左向右移动到屏幕上并从左侧再次将其置于IN

时间:2014-05-28 12:21:08

标签: ios animation uiview

如何将UIView从左向右移动屏幕,然后从左侧再次将其带入屏幕?!!

提前致谢,

5 个答案:

答案 0 :(得分:4)

您所要做的就是为视图的x原点位置设置动画,并使用设备屏幕确定多少。所以对于屏幕右侧:

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;

[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
    _yourView.frame = CGRectMake(screenRect.size.width, _yourView.frame.origin.y, _yourView.frame.size.width, _yourView.frame.size.height);
}
                 completion:^(BOOL finished) {
                     //position screen left after animation
                     _yourView.frame = CGRectMake(-screenRect.size.width, _yourView.frame.origin.y, _yourView.frame.size.width, _yourView.frame.size.height);
                 }];

然后在屏幕上返回0.0 x位置或任何你的起点:

[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
    _yourView.frame = CGRectMake(0.0, _yourView.frame.origin.y, _yourView.frame.size.width, _yourView.frame.size.height);
}
                 completion:^(BOOL finished) {
                     //do something after animation
                 }];

答案 1 :(得分:3)

关闭屏幕:

CGRect frame = viewToMove.frame;
frame.origin.x = self.view.frame.size.width;
viewToMove.frame = frame;

将视图移回屏幕:

CGRect frame = viewToMove.frame;
frame.origin.x = 0;
viewToMove.frame = frame;

答案 2 :(得分:1)

//Store your view's original rect
UIRect originRect = yourView.frame;

[UIView animateWithDuration:0.25 animations:^{

//create new rect and set its X coord to phone's width
CGrect rect = originRect;
rect.origin.x = self.view.frame.size.width;
yourView.frame = rect;

} completion:^(BOOL finished){

   // when animation finished animating start new animation 
   //to bring your view to its original position

   [UIView animateWithDuration:0.25 animation:^{

   yourView.frame = originRect;

   }];

}];

答案 3 :(得分:1)

此代码将在0.3秒内向右移动,然后在接下来的0.3秒内从左向后移动到它的当前位置。

CGRect backToOriginal = Yourview.frame;
CGRect frameOnRight = CGRectMake ( screenWidth,backToOriginal.origin.y,backToOriginal.frame.size.width,backToOriginal.frame.size.height);

[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ Yourview.frame = frameOnRight; } completion:^(BOOL finished)[UIView setFrame: screenLeft;] [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ Yourview.frame = backToOriginal; }{ }];

在语法方面,此代码中必定存在错误,因为我现在不会坐在MAC上。

答案 4 :(得分:1)

//将视图移至左侧

[UIView animateWithDuration:0.5
                      delay:1.0
                    options: UIViewAnimationCurveEaseOut
                 animations:^{
                     self.yourView.frame = CGRectMake(distanceToLeft, distanceFromTop, yourView.width, yourView.height);
                     }
                 completion:^(BOOL finished){
                     NSLog(@"Moved to left!");
                 }];

//移回原位

[UIView animateWithDuration:0.5
                      delay:1.0
                    options: UIViewAnimationCurveEaseOut
                 animations:^{
                     self.yourView.frame = CGRectMake(originalX, OriginalY, yourView.Width, yourView.height);
                 }
                 completion:^(BOOL finished){
                     NSLog(@"Back to normal!");
                 }];