繁体   English   中英

自定义自动旋转iPhone SDK

[英]Custom autorotation iPhone SDK

我想实现自己的自动旋转界面的方式:代码:

// 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;
}

如您所见,我有一个UIView(实际上是UIImageView)-废纸and,并且在更改接口方向时使用CGAffineTransformMakeRotation对其进行旋转。 它的工作原理很好,但当界面方向为默认纵向时,永远不会调用该方法(上下颠倒有效)。 因此,垃圾桶将旋转到以前的方向,而不是默认位置。 我有点担心方法上方的注释(创建UIViewController时会自动添加),上面写着“允许默认纵向以外的其他方向”。

当我返回YES而不是NO时,当然会为每个方向调用该方法,但是我不想为整个界面设置动画,而只是为某些元素设置动画。

请帮助。

-shouldAutorotateToInterfaceOrientation:并不意味着您要执行任何操作,而是返回YES或NO。 这不是告诉您iPhone已经旋转或即将旋转的电话。 这是您的viewController询问的一个问题,以查看它是否允许除当前方向以外的其他方向。

如果您没有视图控制器或子类,则不需要此调用。 无论哪种方式,您都返回YES或NO,然后保留该值。

其余的代码,可以在轻按按钮或从加速度计检测到运动等时使用。

(通常,视图控制器会帮您一个忙,并为您进行旋转,而shouldAutorotateToInterfaceOrientation只是您的批准。如果您没有视图控制器而工作或想自己旋转一些元素,那么您只需在代码中找到合适的时机即可旋转...)

如果只想知道设备的当前方向( 可能与UI的旋转方向不同 ),请使用beginGeneratingDeviceOrientationNotificationsUIDevice orientation方法。

如果要在旋转时更改接口, didRotateFromInterfaceOrientationUIViewController实现didRotateFromInterfaceOrientation

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

暂无
暂无

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

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