解釋錨點(diǎn)最好的例子就是鐘表:
三個(gè)重疊View 苍蔬,通過改變anchorPoint 改變他們的旋轉(zhuǎn)中心麦牺,說白了錨點(diǎn)就是旋轉(zhuǎn)的句柄惰蜜,把手昂拂。
self.containView =[[UIView alloc]init];
self.containView.frame = CGRectMake(200 , 200, 5, 200);
self.containView.backgroundColor =[UIColor redColor];
[self.view addSubview:self.containView];
self.containViewOne =[[UIView alloc]init];
self.containViewOne.frame = CGRectMake(200 , 200, 5, 200);
self.containViewOne.backgroundColor =[UIColor blueColor];
[self.view addSubview:self.containViewOne];
self.containViewTwo =[[UIView alloc]init];
self.containViewTwo.frame = CGRectMake(200 , 200, 5, 200);
self.containViewTwo.backgroundColor =[UIColor greenColor];
[self.view addSubview:self.containViewTwo];
self.containView.layer.anchorPoint = CGPointMake(0.5f, 0.95f);
self.containViewOne.layer.anchorPoint = CGPointMake(0.5f, 0.95f);
self.containViewTwo.layer.anchorPoint = CGPointMake(0.5f, 0.95f);
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(tick) userInfo:nil repeats:YES];
//set initial hand positions
[self tick];
- (void)tick
{
//convert time to hours, minutes and seconds
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger units = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *components = [calendar components:units fromDate:[NSDate date]];
CGFloat hoursAngle = (components.hour / 12.0) * M_PI * 2.0;
//calculate hour hand angle //calculate minute hand angle
CGFloat minsAngle = (components.minute / 60.0) * M_PI * 2.0;
//calculate second hand angle
CGFloat secsAngle = (components.second / 60.0) * M_PI * 2.0;
//rotate hands
self.containView.transform = CGAffineTransformMakeRotation(hoursAngle);
self.containViewOne.transform = CGAffineTransformMakeRotation(minsAngle);
self.containViewTwo.transform = CGAffineTransformMakeRotation(secsAngle);
self.containView.layer.shouldRasterize = YES;
self.containViewOne.layer.shouldRasterize = YES;
self.containViewTwo.layer.shouldRasterize = YES;
}
postion
確切地說,position是layer中的anchorPoint點(diǎn)在superLayer中的位置坐標(biāo)抛猖。因此可以說,position點(diǎn)是相對(duì)suerLayer的口糕,anchorPoint點(diǎn)是相對(duì)layer的昌跌,兩者是相對(duì)不同的坐標(biāo)空間的一個(gè)重合點(diǎn)。
再來看看position的原始定義: The layer’sposition in its superlayer’s coordinate space。 中文可以理解成為position是layer相對(duì)superLayer坐標(biāo)空間的位置吓著,很顯然,這里的位置是根據(jù)anchorPoint來確定的.
**anchorPoint造烁、position搞动、frame
**
anchorPoint的默認(rèn)值為(0.5,0.5),也就是anchorPoint默認(rèn)在layer的中心點(diǎn)驮履。默認(rèn)情況下鱼辙,使用addSublayer函數(shù)添加layer時(shí),如果已知layer的frame值玫镐,根據(jù)上面的結(jié)論倒戏,那么position的值便可以用下面的公式計(jì)算:
position.x = frame.origin.x + 0.5 * bounds.size.width;
position.y = frame.origin.y + 0.5 * bounds.size.height恐似;
里面的0.5是因?yàn)閍nchorPoint取默認(rèn)值杜跷,更通用的公式應(yīng)該是下面的:
position.x = frame.origin.x + anchorPoint.x *bounds.size.width;
position.y = frame.origin.y + anchorPoint.y *bounds.size.height矫夷;
下面再來看另外兩個(gè)問題葛闷,如果單方面修改layer的position位置,會(huì)對(duì)anchorPoint有什么影響呢双藕?修改anchorPoint又如何影響position呢淑趾?
self.containView =[[UIView alloc]init];
self.containView.frame = CGRectMake(200 , 200,100, 200);
self.containView.backgroundColor =[UIColor redColor];
[self.view addSubview:self.containView];
NSLog(@"%@",NSStringFromCGPoint(self.containView.layer.anchorPoint));
self.containView.layer.position = CGPointMake(50, 50);
NSLog(@"%@",NSStringFromCGPoint(self.containView.layer.anchorPoint));
根據(jù)代碼測(cè)試,兩者互不影響忧陪,受影響的只會(huì)是frame.origin扣泊,也就是layer坐標(biāo)原點(diǎn)相對(duì)superLayer會(huì)有所改變。換句話說嘶摊,frame.origin由position和anchorPoint共同決定延蟹,上面的公式可以變換成下面這樣的:
frame.origin.x = position.x - anchorPoint.x *bounds.size.width;
frame.origin.y = position.y - anchorPoint.y *bounds.size.height更卒;
這就解釋了為什么修改anchorPoint會(huì)移動(dòng)layer等孵,因?yàn)閜osition不受影響,只能是frame.origin做相應(yīng)的改變蹂空,因而會(huì)移動(dòng)layer俯萌。
因?yàn)?position點(diǎn)和anchorPoint點(diǎn)是獨(dú)立的果录,自己不會(huì)因?yàn)榱硗庖粋€(gè)的改變而發(fā)生變化
參考文章 (http://blog.sina.com.cn/s/blog_155083d9e0102wi0r.html)
這篇文章描述錨點(diǎn)描述的很清楚(http://www.reibang.com/p/7703e6fc6191)