简体   繁体   English

自定义自动旋转iPhone SDK

[英]Custom autorotation iPhone SDK

I want to implement my own way of autorotating the interface: Code: 我想实现自己的自动旋转界面的方式:代码:

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 NSLog(@"should!");
 [UIView beginAnimations:nil context:NULL];
 [UIView setAnimationDuration:0.4];
 if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
  trash.transform = CGAffineTransformMakeRotation(M_PI*0.5);
 }
 else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
  trash.transform = CGAffineTransformMakeRotation(-M_PI*0.5);
 }
 else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
  trash.transform = CGAffineTransformIdentity;//CGAffineTransformMakeRotation(M_PI);
 }
 else {
  trash.transform = CGAffineTransformIdentity;

 }
 [UIView commitAnimations];
 return NO;
}

As you see I've a UIView (in fact UIImageView) - trash and I'm rotating it using CGAffineTransformMakeRotation when interface orientation is changed. 如您所见,我有一个UIView(实际上是UIImageView)-废纸and,并且在更改接口方向时使用CGAffineTransformMakeRotation对其进行旋转。 It works great except, the method never gets called when the interface orientation is default portrait (up side down works). 它的工作原理很好,但当界面方向为默认纵向时,永远不会调用该方法(上下颠倒有效)。 So the trash is rotated to the previous orientation instead of default. 因此,垃圾桶将旋转到以前的方向,而不是默认位置。 I'm worried a little bit about the comment above the method (automatically added when creating UIViewController), that says "to allow orientations other than the default portrait orientation". 我有点担心方法上方的注释(创建UIViewController时会自动添加),上面写着“允许默认纵向以外的其他方向”。

When I return the YES instead of NO of course the method is called for every orientation, but I don't want to animate the whole interface, but only some elements. 当我返回YES而不是NO时,当然会为每个方向调用该方法,但是我不想为整个界面设置动画,而只是为某些元素设置动画。

Plz help. 请帮助。

-shouldAutorotateToInterfaceOrientation: is not meant for you to do anything but to return YES or NO. -shouldAutorotateToInterfaceOrientation:并不意味着您要执行任何操作,而是返回YES或NO。 It is not a call to tell you that the iPhone has been rotated, or will be rotated. 这不是告诉您iPhone已经旋转或即将旋转的电话。 It is a question being asked of your viewController to see if it would allow for an orientation other than the current one. 这是您的viewController询问的一个问题,以查看它是否允许除当前方向以外的其他方向。

If you don't have a view controller or subclass, then you're not going to need this call. 如果您没有视图控制器或子类,则不需要此调用。 Either way, you return YES or NO, and leave it at that. 无论哪种方式,您都返回YES或NO,然后保留该值。

The rest of your code, you can use when a button is tapped, or if you detect movement from the accelerometer, etc. 其余的代码,可以在轻按按钮或从加速度计检测到运动等时使用。

(Generally view controllers do you a favor and do the rotation for you, and the shouldAutorotateToInterfaceOrientation is just your approval. If you are working without view controllers or want to rotate some elements yourself, then you just find the right moment in your code to do the rotation...) (通常,视图控制器会帮您一个忙,并为您进行旋转,而shouldAutorotateToInterfaceOrientation只是您的批准。如果您没有视图控制器而工作或想自己旋转一些元素,那么您只需在代码中找到合适的时机即可旋转...)

If you just want to know current orientation of the device ( which may be different than rotation of the UI! ), use beginGeneratingDeviceOrientationNotifications and orientation methods of UIDevice . 如果只想知道设备的当前方向( 可能与UI的旋转方向不同 ),请使用beginGeneratingDeviceOrientationNotificationsUIDevice orientation方法。

If you want to change interface when it rotates, implement didRotateFromInterfaceOrientation in UIViewController . 如果要在旋转时更改接口, didRotateFromInterfaceOrientationUIViewController实现didRotateFromInterfaceOrientation

我只想在用户更改设备方向时旋转子视图,那么您会要求什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM