標籤:
項目中用到了調用自拍的功能,然後拍出來的照片需要處理後才能讓人臉朝上。剛開始我用重力感應去判斷手機當時的方向,效果很好,不管手機是橫放還是豎放,拍出來的照片始終是頭朝上的,然而有一部手機在測試的時候出現了bug,只要使用了重力感應退出去的時候就會崩潰。後來將重力感應關閉後就不再崩潰了。重力感應是iOS4.0之前的方法,iOS5.0後用coremotion代替了重力感應,這種架構也能夠監聽手機的狀態,然而我發現還是會崩潰,不過很難重現,於是準備放棄這種方法。畢竟只要出現一次崩潰就說明代碼有問題,這是不允許的,畢竟是公司的項目,本人還是很有職業道德的。說到這裡並沒有進入今天的主題,只是想把之前遇到的坑拿出來分享一下,讓那些遇到坑的夥伴們感到欣慰,因為還有很多爬坑的人啊(就像我)!
今天的主題是圖片載入動畫,之所以寫這篇部落格,一來是好久沒有寫了,而來是想以後長期使用,比較簡單的動畫效果,各位大神獻醜了。
話不多說,直接上代碼。
1 #import <UIKit/UIKit.h> 2 3 @interface LMFLoadingView : UIView 4 5 #pragma mark --設定半徑 6 - (void)setRadiu:(CGFloat)radiu; 7 8 #pragma mark --設定點的個數,12或8個 9 - (void)setPointNumber:(NSInteger)pointNumber;10 11 #pragma mark --判斷是否畫圖12 - (void)createUI;13 14 #pragma mark --畫圖15 - (void)drawRect;16 17 @end
有些參數可以自行設定,該動畫比較簡單,有興趣的夥伴可以隨意改動。.m檔案如下:
1 #import "LMFLoadingView.h" 2 3 @interface LMFLoadingView () 4 @property (nonatomic, assign) CGFloat radiu; 5 @property (nonatomic, assign) NSInteger pointNumber; 6 @property (nonatomic, strong) UIColor *pointColor; 7 @property (nonatomic, assign) CGFloat circleRadiu; 8 @end 9 10 @implementation LMFLoadingView 11 12 - (instancetype)initWithFrame:(CGRect)frame 13 { 14 if (self = [super initWithFrame:frame]) 15 { 16 self.backgroundColor = [UIColor blackColor]; 17 self.pointColor = [UIColor whiteColor]; 18 self.circleRadiu = self.frame.size.width/2.0 - 10; 19 self.radiu = 1; 20 self.pointNumber = 12; 21 } 22 return self; 23 } 24 25 - (void)setRadiu:(CGFloat)radiu 26 { 27 _radiu = radiu; 28 [self createUI]; 29 } 30 31 - (void)setPointNumber:(NSInteger)pointNumber 32 { 33 if (pointNumber < 12) 34 { 35 _pointNumber = 8; 36 } 37 else 38 { 39 _pointNumber = 12; 40 } 41 [self createUI]; 42 } 43 44 - (void)createUI 45 { 46 if (self.radiu > 0 && (self.pointNumber == 8 || self.pointNumber == 12)) 47 { 48 NSArray *views = [self subviews]; 49 for (UIView *obj in views) 50 { 51 [obj removeFromSuperview]; 52 } 53 [self drawRect]; 54 } 55 } 56 57 - (void)drawRect 58 { 59 CGFloat angle = 90/((self.pointNumber/4.0-1)+1) * M_PI/180; 60 for (int i=0; i<self.pointNumber; i++) 61 { 62 float x = self.circleRadiu * sinf(i*angle) + self.circleRadiu+10; 63 float y = self.circleRadiu * cosf(i*angle) + self.circleRadiu+10; 64 UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.radiu*2, self.radiu*2)]; 65 view.image = [UIImage imageNamed:@"big_yellow_point"]; 66 // view.layer.masksToBounds = YES; 67 // view.layer.cornerRadius = self.radiu; 68 [view setCenter:CGPointMake(x, y)]; 69 // view.backgroundColor = self.pointColor; 70 [self addSubview:view]; 71 [self performSelector:@selector(startAnimat:) withObject:view afterDelay:i/12.0]; 72 } 73 } 74 75 #pragma mark --畫一張 76 77 - (void)startAnimat:(UIView *)view 78 { 79 CGPoint point = view.center; 80 81 [UIView animateWithDuration:0.5 animations:^{ 82 83 view.frame = CGRectMake(0, 0, 5, 5); 84 [view setCenter:point]; 85 86 } completion:^(BOOL finished) { 87 88 [UIView animateWithDuration:0.5 animations:^{ 89 90 view.frame = CGRectMake(0, 0, self.radiu, self.radiu); 91 [view setCenter:point]; 92 93 } completion:^(BOOL finished) { 94 [self startAnimat:view]; 95 }]; 96 97 }]; 98 } 99 100 @end
圖片載入動畫