项目中用到了调用前置摄像头的功能,然后拍出来的照片需要处理后才能让人脸朝上。刚开始我用重力感应去判断手机当时的方向,效果很好,不管手机是横放还是竖放,拍出来的照片始终是头朝上的,然而有一部手机在测试的时候出现了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