一 佈局
frame:代表了圖層的外部座標(相對於父視圖);
bounds:內部座標相對於自己;
center,positinon:都代表了相對不父圖層錨點的位置。
發生旋轉的時候frame會改變bounds,center,positinon,不會改變
二 錨點
anchorPoint:錨點用來移動圖層的把柄,默認位於圖層的中心。錨點可以被移動,frame也會相對移動
通過一個鐘錶小示例演示下錨點作用。
xib佈局UI界面,時分秒針重合了,
- (void)viewDidLoad {
[super viewDidLoad];
self.panVIew.layer.cornerRadius = 150;
// self.panVIew.layer.anchorPoint = CGPointMake(0, 0);改變錨點frame會變;
[self tick];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(tick) userInfo:nil repeats:YES];
}
-(void)tick{
NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSCalendarUnit units = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *compents = [calender components:units fromDate:[NSDate date]];
self.time.text = [NSString stringWithFormat:@"%ld:%ld:%ld",(long)compents.hour,(long)compents.minute,(long)compents.second];
CGFloat hourAngle = (compents.hour/12.0)*M_PI*2.0;
CGFloat minuteAngle = (compents.minute/60.0)*M_PI*2.0;
CGFloat secondAngle = (compents.second/60.0)*M_PI*2.0;
self.hourView.transform = CGAffineTransformMakeRotation(hourAngle);
self.minuteView.transform = CGAffineTransformMakeRotation(minuteAngle);
self.secondView.transform = CGAffineTransformMakeRotation(secondAngle);
}
效果:
很明顯指針是圍繞着中心旋轉的,需要改變旋轉的錨點,可以加到viewDidLoad裏面
self.hourView.layer.anchorPoint = CGPointMake(0.5f, 0.9f);
self.minuteView.layer.anchorPoint = CGPointMake(0.5f, 0.9f);
self.secondView.layer.anchorPoint = CGPointMake(0.5f, 0.9f);
看下改變後的效果: