开发者

How can I work with orientation?

I have did coding as in ViewDidLoad as below

    // 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.glowAmo开发者_运维百科unt = 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];
 }

In simple it will display the current time with glow on it, well I want to know that how can I code while i want to rotate iphone, it should rotate with it ...


To enable rotation you need to implement the following method in your view controller:

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

...which would support all orientations. Note that on iPhone you may only want to support 3 (upside-down portrait is not typically supported on iPhone), so you can return YES or NO as appropriate.

Once you've implemented that method your view controller should now rotate in response to orientation changes. However, your view may still need to resize.

For this you can either use autoresizing masks on each view (to allow them to stretch/shrink or move as appropriate), or adjust their frames manually by implementing the willAnimateRotationToInterfaceOrientation:duration: method. The former is generally the better approach, although there will probably be some instances where you'll need to manually adjust frames, or add/remove entire views if your interface substantially changes in landscape.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜