覆盖设备旋转时 UIViewController 的默认动画

发布于 2025-01-03 20:42:00 字数 150 浏览 2 评论 0原文

我想在设备旋转时提供自定义动画,完全覆盖默认动画。实现这一目标的最佳方法是什么?

顺便说一句,我计划的动画类型是:

a)当设备进入横向时,有一个新的视图从顶部滑动,就像它正在下降一样。

b) 当设备返回纵向时,该视图应向下滑动并消失。

I want to provide a custom animation when the device rotates, completely overriding the default one. What's the best way to achieve this?

BTW, the kind of animation I'm planning is:

a) When the device goes into landscape, have a new view slide from the top as if it was falling.

b) When the device goes back to portrait, that view should slide down and dissappear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽袖吟 2025-01-10 20:42:00

最佳是主观的,取决于您的整个应用程序。

处理轮换事件的一种相当直接的方法是告诉系统不要这样做并自己处理它们。对于您想要的效果,即当设备旋转到侧面时,基本上相当于从侧面滑动(预旋转)视图,这似乎是合适的。

这是如何实现这种效果的一个非常基本的示例。

@implementation B_VCRot_ViewController // defined in .h as @interface B_VCRot_ViewController : UIViewController
@synthesize sideways; // defined in .h as @property (strong, nonatomic) IBOutlet UIView *sideways;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void)orientationChange:(NSNotification *)note{
    UIDeviceOrientation newOrientation = [[UIDevice currentDevice] orientation];
    CGSize sidewaysSize = self.sideways.frame.size;
    if (newOrientation == UIDeviceOrientationLandscapeLeft){
        [UIView animateWithDuration:1.0 animations:^{
            self.sideways.frame = CGRectMake(0, 0, sidewaysSize.width, sidewaysSize.height);
        }];
    }
    else {
        [UIView animateWithDuration:1.0 animations:^{
            self.sideways.frame = CGRectMake(self.view.bounds.size.width, 0, sidewaysSize.width, sidewaysSize.height);
        }];
    }
}
- (void)viewDidLoad{
    [super viewDidLoad];
    [self.view addSubview:sideways];
    self.sideways.transform = CGAffineTransformMakeRotation(M_PI_2); // Rotates the 'sideways' view 90deg to the right.
    CGSize sidewaysSize = self.sideways.frame.size;
    // Move 'sideways' offscreen to the right to be animated in on rotation.
    self.sideways.frame = CGRectMake(self.view.bounds.size.width, 0, sidewaysSize.width, sidewaysSize.height);
    // register for rotation notifications
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
    // Do any additional setup after loading the view, typically from a nib.
}
@end

我在这里所做的是在 .xib 中添加一个横向的 UIView 并将其连接到名为 sideways< 的 IBOutlet /代码>。在 viewDidLoad 中,我将其添加为子视图,预旋转它,并将其移出屏幕。我还将自己添加为设备旋转通知的观察者(请记住稍后将自己删除以接收该通知)。在 shouldAutoRo.. 中,我指出该 VC 仅处理纵向。

当设备旋转时,NSNotificationCenter 调用orientationChange:。此时,如果设备向左旋转,我的侧视图将从右侧滑入(看起来像是向下滑动)。

显然,对于两种横向方向,代码都会更复杂。此外,您可能必须调整动画时间,以使第二个视图感觉像是“下落”

Best is subjective and dependent upon your application as a whole.

One fairly straight-forward way of handling the rotation events is telling the system not to and handling them yourself. For your desired effect of what essentially amounts to sliding a (pre-rotated) view in from the side when the device is rotated to the side, this would seem appropriate.

Here is a very basic sample of how to achieve such an effect.

@implementation B_VCRot_ViewController // defined in .h as @interface B_VCRot_ViewController : UIViewController
@synthesize sideways; // defined in .h as @property (strong, nonatomic) IBOutlet UIView *sideways;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void)orientationChange:(NSNotification *)note{
    UIDeviceOrientation newOrientation = [[UIDevice currentDevice] orientation];
    CGSize sidewaysSize = self.sideways.frame.size;
    if (newOrientation == UIDeviceOrientationLandscapeLeft){
        [UIView animateWithDuration:1.0 animations:^{
            self.sideways.frame = CGRectMake(0, 0, sidewaysSize.width, sidewaysSize.height);
        }];
    }
    else {
        [UIView animateWithDuration:1.0 animations:^{
            self.sideways.frame = CGRectMake(self.view.bounds.size.width, 0, sidewaysSize.width, sidewaysSize.height);
        }];
    }
}
- (void)viewDidLoad{
    [super viewDidLoad];
    [self.view addSubview:sideways];
    self.sideways.transform = CGAffineTransformMakeRotation(M_PI_2); // Rotates the 'sideways' view 90deg to the right.
    CGSize sidewaysSize = self.sideways.frame.size;
    // Move 'sideways' offscreen to the right to be animated in on rotation.
    self.sideways.frame = CGRectMake(self.view.bounds.size.width, 0, sidewaysSize.width, sidewaysSize.height);
    // register for rotation notifications
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
    // Do any additional setup after loading the view, typically from a nib.
}
@end

What I have done here is added a UIView with a landscape orientation in the .xib and connected it to an IBOutlet named sideways. In viewDidLoad I add it as a subview, pre-rotate it, and move it offscreen. I also add self as an observer for the device rotation notifications (Remember to remove yourself later for that notification). And in shouldAutoRo.. I indicate that this VC only handles portrait.

When the device rotates NSNotificationCenter calls orientationChange:. At that point if the device is rotated to the left, my sideways view will slide in from the right (which seems like it's sliding down).

Obviously for both landscape orientations the code would be more complex. Also you would likely have to mess around with the animation timing to make it feel as if the second view is "falling"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文