我如何处理方向?

时间:2011-07-07 10:55:52

标签: iphone orientation

我在ViewDidLoad中进行了编码,如下所示

    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {
    [super viewDidLoad];

    UIColor *colorOne = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
    UIColor *colorTwo = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];

   //Create the gradient and add it to our view's root layer
   CAGradientLayer *gradientLayer = [[[CAGradientLayer alloc] init] autorelease];
   gradientLayer.frame = CGRectMake(0.0, 0.0, 320.0, 480.0);
   [gradientLayer setColors:[NSArray arrayWithObjects:(id)colorOne.CGColor,    (id)colorTwo.CGColor, nil]];
   [self.view.layer insertSublayer:gradientLayer atIndex:0];

date1 = [[RRSGlowLabel alloc]     initWithFrame:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/2, 300, 480)];
   [date1 setTextAlignment:UITextAlignmentCenter];
   date1.center = CGPointMake((self.view.frame.size.width/2)-20, (self.view.frame.size.height/2)-100);
   date1.textColor = [UIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0];
   date1.font=[UIFont fontWithName:@"ds-digital" size:20.0];
    //[label sizeToFit];
   [date1 setBackgroundColor:[UIColor clearColor]];

   date1.glowOffset = CGSizeMake(0.0, 0.0);
   date1.glowColor = date1.textColor;
       date1.glowAmount = 120.0f;
  dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
   [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
   currentDate = [NSDate date];
   //[dateFormatter setDateFormat:@"hh:mm"];
   NSString *date = [dateFormatter stringFromDate:currentDate];
   [self.date1 setText:date];   
   [self.view addSubview:date1];

   time = [[RRSGlowLabel alloc]     initWithFrame:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/2, 300,  480)];
[time setTextAlignment:UITextAlignmentCenter];
time.center = CGPointMake((self.view.frame.size.width/2)-20, self.view.frame.size.height/2);
time.textColor = [UIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0];
time.font=[UIFont fontWithName:@"ds-digital" size:80.0];
//[label sizeToFit];
[time setBackgroundColor:[UIColor clearColor]];

time.glowOffset = CGSizeMake(0.0, 0.0);
time.glowColor = time.textColor;
    time.glowAmount = 120.0f;
currentDate = [NSDate date];
[dateFormatter setDateFormat:@"hh:mm"];
date = [dateFormatter stringFromDate:currentDate];
[self.time setText:date];   
[self.view addSubview:time];



seconds = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2,     self.view.frame.size.height/2, 300, 480)];
[seconds setTextAlignment:UITextAlignmentCenter];
seconds.center = CGPointMake((self.view.frame.size.width/2)+105, (self.view.frame.size.height/2)+20);
seconds.textColor = [UIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0];
seconds.font=[UIFont fontWithName:@"ds-digital" size:20.0];
currentDate = [NSDate date];
[dateFormatter setDateFormat:@"ss a"];
date = [dateFormatter stringFromDate:currentDate];
[seconds setText:date];
//[label sizeToFit];
[seconds setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:seconds];

date = nil;
dateFormatter = nil;
currentDate = nil;

timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self    selector:@selector(updateTime) userInfo:nil repeats:YES];
 }

简单地说它会显示当前时间点亮它,我想知道如何在我想要旋转iphone时进行编码,它应该随之旋转......

1 个答案:

答案 0 :(得分:1)

要启用旋转,您需要在视图控制器中实现以下方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

...支持所有方向。请注意,在iPhone上您可能只想支持3(iPhone上通常不支持颠倒的肖像),因此您可以根据需要返回YESNO

一旦实现了该方法,您的视图控制器现在应该旋转以响应方向更改。但是,您的视图可能仍需要调整大小。

为此,您可以在每个视图上使用自动调整遮罩(允许它们拉伸/缩小或适当移动),或者通过实施willAnimateRotationToInterfaceOrientation:duration:方法手动调整其框架。前者通常是更好的方法,尽管在某些情况下您可能需要手动调整帧,或者如果您的界面在横向上发生重大变化,则添加/删除整个视图。