作業目標:Youtube操作影片
練習原始碼:homework0803
這個作業主要是在練習對view以及簡單的繪圖的操作,還有timer的使用
因此我截錄一些我覺得是練習關鍵的程式碼
下面的是viewController的相關程式碼
一個個出現的程式碼,按下show按鈕時觸發
1 2 3 4 5 6 7 8 | - (IBAction)showButtonClick { [self clearButtonClick]; [self.circleNumber resignFirstResponder]; total = [self.circleNumber.text integerValue]; index = 0; //設定間隔一個個產生circle [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(addItems:) userInfo:NULL repeats:YES]; } |
取得圓圈排列坐標的程式碼,傳入值為圓半徑及角度
1 2 3 4 5 6 7 8 | //取得坐標 - (CGPoint)getPoint:( int ) radius withAngel:(CGFloat) degrees{ int y = sin (degrees * M_PI / 180)*radius; int x = cos (degrees * M_PI / 180)*radius; CGPoint btCorner = CGPointMake(x, y) ; return btCorner; } |
下面的程式碼則是在被新增的UIView物件裡面
顯示動畫,這邊要注意的是因為定位點的關係,由於我們希望元件的縮放是以view的中心來做縮放
所以改變的值是self.bounds,一般我們在做元件內部繪圖事件,都會使用self.bounds
而self.frame則是在外部設定物件資訊時使用。
01 02 03 04 05 06 07 08 09 10 11 12 13 | -( void ) playAnimation{ [UIView animateWithDuration:0.5f animations:^{ self.bounds = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, 50, 50); }completion:^( BOOL finished) { [UIView animateWithDuration:0.5f animations:^{ self.bounds = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, 30, 30); }completion:^( BOOL finished) { }]; }]; } |
另外,因為- (id)initWithFrame:(CGRect)frame被呼叫的時間是在circle被新建的時後,而不是在被加到畫面時,
為了落實動畫的被執行時間,所以我又實作了layoutSubviews這個方法,
並加上BOOL init當今天是第一次被呼叫時,才會執行playAnimation
1 2 3 4 5 6 | -( void )layoutSubviews{ if (init){ [self playAnimation]; init = NO; } } |
然後實作按下後縮小離開時恢復大小的功能
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 | -( void ) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [UIView animateWithDuration:0.2f animations:^{ self.bounds = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, 0, 0); } completion:^( BOOL finished) { }]; } -( void ) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ [UIView animateWithDuration:0.2f animations:^{ self.bounds = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, 30, 30); } completion:^( BOOL finished) { }]; } |
原始碼下載:homework0803