Circlelayout 模擬一個(gè)時(shí)鐘

##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;

效果:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容