##UICollectionViewLayout寫(xiě)一個(gè)Layout繼承自UICollectionViewLayout哥桥,實(shí)現(xiàn)以下方法://返回cell的Layout屬性- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;//返回SupplementaryView的Layout屬性(HeaderView绑谣、FooterView)- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath;//返回DecorationView的Layout屬性- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString*)elementKind atIndexPath:(NSIndexPath *)indexPath;實(shí)現(xiàn)prepareLayout方法设易,完成布局。在prepareLayout方法中調(diào)用[super prepareLayout];-(void)prepareLayout{? ? [super prepareLayout];? ? [self.attrsArr removeAllObjects];? ? //注冊(cè)Decoration class? ? [self registerClass:[DecorationView class] forDecorationViewOfKind:@"DecorationView"];//注冊(cè)Decoration View? ? }? ? layoutAttributesForElementsInRect方法中添加需要布局的所有Elements的Attributes? ? -(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect{? ? NSInteger? count=[self.collectionView numberOfItemsInSection:0];? ? //添加DecorationView的LayoutAttributes? ? [self.attrsArr addObject:[self layoutAttributesForDecorationViewOfKind:@"DecorationView" atIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]];? ? for (int i=0; i, <#CGFloat ty#>):只能變化一次崔挖,因?yàn)檫@種方式的變化始終是以最原始的狀態(tài)值進(jìn)行變化的案站,所以只能變化一次
UIButton *head = (UIButton *) [self.view viewWithTag:10];
head.transform = CGAffineTransformMakeTranslation(0,-10);
(2)CGAffineTransformTranslate(CGAffineTransform t, <#CGFloat tx#>, <#CGFloat ty#>):能夠多次變化痛黎,每次變化都是以上一次的狀態(tài)(CGAffineTransform t)進(jìn)行的變化,所以可以多次變化
head.transform = CGAffineTransformTranslate(head.transform, 0, -10);
(3) CGAffineTransformIdentity:清空所有的設(shè)置的transform(一般和動(dòng)畫(huà)配合使用以躯,只能使用于transfofrm設(shè)置的畫(huà)面)
UIButton *head = (UIButton *) [self.view viewWithTag:10];
head.transform = CGAffineTransformIdentity;
(4)CGAffineTransformMakeScale( CGFloat? sx,? CGFloat? sy) (縮放:設(shè)置縮放比例)僅通過(guò)設(shè)置縮放比例就可實(shí)現(xiàn)視圖撲面而來(lái)和縮進(jìn)頻幕的效果槐秧。
UIButton *head = [self.view viewWithTag:10];
head.transform = CGAffineTransformScale(head.transform,1.5,1.5);
(5) CGAffineTransformMakeRotation( CGFloat? angle) (旋轉(zhuǎn):設(shè)置旋轉(zhuǎn)角度)
UIButton *head =? [self.view viewWithTag:10];
head.transform = CGAffineTransformMakeRotation(M_PI_2);
###實(shí)現(xiàn)UIView繞固定點(diǎn)旋轉(zhuǎn)
定義方法:
CGAffineTransform? GetCGAffineTransformRotateAroundPoint(UIView *view,float centerX, float centerY ,float x ,float y ,float angle){
//centerX ,centerY 為當(dāng)前View的中心點(diǎn)
x = x - centerX;
y = y - centerY;
CGAffineTransform? trans = CGAffineTransformTranslate(view.transform,x, y);
trans = CGAffineTransformRotate(trans,angle);
trans = CGAffineTransformTranslate(trans,-x, -y);
return trans;
}
使用如下:
float centerSecondX = self.secondHand.bounds.size.width/2;
float centerSecondY = self.secondHand.bounds.size.height/2;
float xSecond = self.secondHand.bounds.size.width/2;
float ySecond = self.secondHand.bounds.size.height;
CGAffineTransform transSecond = GetCGAffineTransformRotateAroundPoint(self.secondHand,centerSecondX,centerSecondY ,xSecond,ySecond,1*second/30.0*M_PI);
self.secondHand.transform = transSecond;
效果: