【问题标题】:custom alert view does not rotate toward device rotation自定义警报视图不会向设备旋转方向旋转
【发布时间】:2010-11-26 03:19:45
【问题描述】:

我从

实现了一个自定义警报视图

http://iphonedevelopment.blogspot.com/2010/05/custom-alert-views.html

但我想让该自定义警报视图在设备方向更改时旋转。当我第一次以 iPhone 模拟器的纵向运行该应用程序时,自定义警报视图方向是横向,但是当我将设备旋转到左侧横向时,自定义警报视图方向是横向。在以多种方式旋转设备后,我发现自定义警报方向与主页按钮位置一致..

我实现了这几种方法:

-(void) willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration {
NSLog(@"kkksksksk");    

}

-(void) willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
 NSLog(@"aaa"); 

}

-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
 NSLog(@"bbb");

}

-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
 NSLog(@"bbb");

}

-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
NSLog(@"ccc");

}

-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return YES;
NSLog(@"shouldAutorotateToInterfaceOrientation");

}

但是没有人被召唤... 谁能帮我弄清楚为什么自定义警报视图没有像我想要的那样旋转?

更新: 我是这样实现 viewWillAppear 方法的

-(void)viewWillAppear:(BOOL)animated {

UIInterfaceOrientation toOrientation = self.interfaceOrientation;
if(toOrientation == UIInterfaceOrientationPortrait){
    CGAffineTransform affine = CGAffineTransformMakeRotation (M_PI * 180 / 180.0f);
    NSLog(@"vwa : portrait upside down");

    [self.alertView setTransform:affine];
    [self.backgroundView setTransform:affine];

    //[self.view setTransform:affine];
    self.view.center = CGPointMake(160, 240);
    NSLog(@"affine : %f", affine);

}else if(toOrientation == UIInterfaceOrientationPortraitUpsideDown){
    CGAffineTransform affine = CGAffineTransformMakeRotation (0.0);

    NSLog(@"vwa : Portrait ");

    [self.alertView setTransform:affine];
    [self.backgroundView setTransform:affine];
    self.view.center = CGPointMake(160, 240);
    NSLog(@"affine : %f", affine);
}else if(toOrientation == UIInterfaceOrientationLandscapeLeft){
    CGAffineTransform affine = CGAffineTransformMakeRotation (M_PI * 270 / 180.0f);

    [self.alertView setTransform:affine];
    [self.backgroundView setTransform:affine];
    self.view.center = CGPointMake(160, 240);

    NSLog(@"vwa : landscpae left");

    NSLog(@"affine : %f", affine);
}else{
    CGAffineTransform affine = CGAffineTransformMakeRotation ( M_PI * 90 / 180.0f);
    [self.alertView setTransform:affine];
    [self.backgroundView setTransform:affine];
    self.view.center = CGPointMake(160, 240);

    NSLog(@"vwa : landscape right");
    NSLog(@"affine : %f", affine);
}

}

但是在第一次启动应用程序时条件刚刚满足,之后,即使我多次旋转设备,也没有调用条件和语句

【问题讨论】:

  • 你应该格式化你的代码...
  • “你应该格式化你的代码”是什么意思?对不起,我不明白
  • 本网站支持代码格式,您应该使用这种格式。您可以在底部视图中验证编辑结果。

标签: iphone rotation


【解决方案1】:

完成了... 见About the orientation of iPhone 找到答案:)

【讨论】:

    【解决方案2】:

    UIView 不能接受 UIViewController 的委托消息。所以你应该注册为观察者来接收UIDeviceOrientationDidChangeNotification 通知。

    【讨论】:

    • 对不起..我不明白如何注册成为观察员,你能描述一下吗?
    • toro ...您的意思是像stackoverflow.com/questions/3829756/… 上的答案一样注册为观察者吗?是你的吗? :)
    猜你喜欢
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    相关资源
    最近更新 更多