从左到右将 UIView 移出屏幕并从左再次将其移入

Posted

技术标签:

【中文标题】从左到右将 UIView 移出屏幕并从左再次将其移入【英文标题】:Moving UIView OFF the screen from left to right and bring it IN again from left 【发布时间】:2014-05-28 12:21:07 【问题描述】:

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

提前致谢,

【问题讨论】:

你能单独做这两件事吗? 【参考方案1】:

您所要做的就是为视图的 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
                 ];

【讨论】:

【参考方案2】:

离开屏幕:

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;

【讨论】:

这会将它从屏幕上移到右侧。根据他们希望它从屏幕上移出的位置,他们会相应地调整 x 或 y。【参考方案3】:
//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;

   ];

];

【讨论】:

稍微清理一下,这对我来说是最简单的。【参考方案4】:

此代码将在 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 上。

【讨论】:

【参考方案5】:

//向左移动视图

[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!");
                 ];

【讨论】:

以上是关于从左到右将 UIView 移出屏幕并从左再次将其移入的主要内容,如果未能解决你的问题,请参考以下文章

你如何使 UIView 的幻灯片从左到右并在 iPhone 中相互对接?

UICollectionView 从左到右读取多行

如何为多个屏幕设置从左到右和反转的动画师

UIImageView从左到右改变动画

过渡画面从左到右?

从上到下,从左到右对页面(2D 平面)上的框进行排序。 (Python)