无聊的时候乱搞的一个小动画,先上效果图
说明:根据手指移动产生不同颜色的气泡并且向不同的方向移动。
1、手指移动使用的方法是:
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
2、气泡移动使用的定时器CADisplayLink
CADisplayLink * display = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateCirclePosition)];
[display addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];//添加到runloop中
说明:CADisplayLink是用于同步屏幕刷新频率的计时器,有兴趣的童鞋可以百度深入学习。
3、画圈圈要在view里面调用
- (void)drawRect:(CGRect)rect
下面开始上完整的代码:
1、创建view,然后在ViewController里面初始化此view
- (YLAnimationView *)animationView {
if (!_animationView) {
_animationView = [[YLAnimationView alloc] initWithFrame:self.view.bounds];
}
return _animationView;
}
2、首先在ViewController里面调用touchesMoved方法;说明把获取到屏幕上的点传给view
UITouch *touch = [touches anyObject];
CGPoint position = [touch locationInView:self.view];
self.animationView.circleX = position.x;
self.animationView.circleY = position.y;
[self.animationView moveCircle];
}
3、在view中创建几个可变数组用来添加位置、颜色、透明度、随机数(每个气泡随机移动的值)、符号数组(用来区别移动的放向)
@property (nonatomic, strong) NSMutableArray *colorArray;//随机颜色数组
@property (nonatomic, strong) NSMutableArray *positionArray;//位置数组
@property (nonatomic, strong) NSMutableArray *symbolArray;//符号数组
@property (nonatomic, strong) NSMutableArray *opticArray;//透明度数组
@property (nonatomic, strong) NSMutableArray *randomArray;//随机数数组
@property (nonatomic, strong) CADisplayLink * display;//CADisplayLink是用于同步屏幕刷新频率的计时器
@property (nonatomic, assign) BOOL status;//判断状态
初始化数组
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor whiteColor];
self.positionArray = [NSMutableArray arrayWithCapacity:0];
self.colorArray = [NSMutableArray arrayWithCapacity:0];
self.symbolArray = [NSMutableArray arrayWithCapacity:0];
self.opticArray = [NSMutableArray arrayWithCapacity:0];
self.randomArray = [NSMutableArray arrayWithCapacity:0];
self.status = NO;
}
return self;
}
根据移动的点的位置,添加数据
- (void)moveCircle {
if (!self.display) {//如果定时器存在就不创建,不存在则创建
CADisplayLink * display = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateCirclePosition)];
self.display = display;
[display addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];//添加到runloop中
}
NSNumber *pointX = [NSNumber numberWithFloat:self.circleX];
NSNumber *pointY = [NSNumber numberWithFloat:self.circleY];
[self.positionArray addObject:@[pointX, pointY]];//中心点
//填充颜色
float random_r = arc4random()%256/255.0;
float random_g = arc4random()%256/255.0;
float random_b = arc4random()%256/255.0;
[self.colorArray addObject:@[@(random_r), @(random_g), @(random_b)]];//颜色
[self.opticArray addObject:@(1.0)];//透明度
int symbol = arc4random()%100;
[self.symbolArray addObject:@(symbol)];//符号
int positionX = arc4random()%2+1;
int positionY = arc4random()%2+1;
[self.randomArray addObject:@[@(positionX), @(positionY)]];//随机数数组
}
定时器事件
- (void)updateCirclePosition {
if (self.positionArray.count <= 0&&self.status == YES) {//这个判断实际上没有用
//移除CADisplayLink定时器
//1、从runloop中移除 会将接收者从给定的模式中移除,这个方法会对计时器进行隐式的release.在调用removeFromRunloop方法,需要做判断,如果当期计时器不在runloop的话,会出现野指针的crash.出现crash的原因是runloop多次调用了release方法,进行了over-release.
// [self.display removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
//2、从所有模式中移除 - (void)invalidate是从runloop所有模式中移除计时器,并取消计时器和target的关联关系.多次调用这个方法,不会出现crash
[self.display invalidate];
self.display = nil;
NSLog(@"她要走了");
return;
}else {
if (self.positionArray.count) {
[self setNeedsDisplay];
}else {
[self.display invalidate];
self.display = nil;
//定时器释放后,清空数组;理论上在画图的时候会每次清,为了保险起见
[self.colorArray removeAllObjects];
[self.randomArray removeAllObjects];
[self.opticArray removeAllObjects];
[self.positionArray removeAllObjects];
[self.symbolArray removeAllObjects];
NSLog(@"她要走了吗");
}
}
}
开始画图
- (void)drawRect:(CGRect)rect {
[self drawRectGraphocs:rect];
}
- (void)drawRectGraphocs:(CGRect)rect {
for (int i = 0; i < self.positionArray.count; i++) {
NSArray *pointArray = self.positionArray[i];
float posi_x = [pointArray[0] floatValue];
float posi_y = [pointArray[1] floatValue];
NSArray *colorArray = self.colorArray[i];
NSArray *randomArray = self.randomArray[i];
if (posi_y >= ScreenHeight || posi_x >= ScreenWidth || posi_x <= -50 || posi_y <= -50) {
[self.positionArray removeObjectAtIndex:i];
[self.colorArray removeObjectAtIndex:i];
[self.opticArray removeObjectAtIndex:i];
[self.randomArray removeObjectAtIndex:i];
[self.symbolArray removeObjectAtIndex:i];
if (self.positionArray == 0) {
self.status = YES;
}
}else {
//判断是移动方向
if ([self.symbolArray[i] intValue] < 25) {
posi_x -= (double)[randomArray[0] intValue];
posi_y -= (double)[randomArray[1] intValue];
}else if ([self.symbolArray[i] intValue] >= 25 && [self.symbolArray[i] intValue] < 50){
posi_x -= (double)[randomArray[0] intValue];
posi_y += (double)[randomArray[1] intValue];
}else if ([self.symbolArray[i] intValue] >= 50 && [self.symbolArray[i] intValue] < 75) {
posi_x += (double)[randomArray[0] intValue];
posi_y += (double)[randomArray[1] intValue];
}else {
posi_x += (double)[randomArray[0] intValue];
posi_y -= (double)[randomArray[1] intValue];
}
float a = [self.opticArray[i] floatValue];
a = a*0.96;
[self.opticArray replaceObjectAtIndex:i withObject:@(a)];
[self.positionArray replaceObjectAtIndex:i withObject:@[@(posi_x), @(posi_y)]];
//开始画圆
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(context, CGRectMake(posi_x, posi_y, 50, 50));
[[UIColor colorWithRed:[colorArray[0] floatValue] green:[colorArray[1] floatValue] blue:[colorArray[2] floatValue] alpha:a] set];
CGContextFillPath(context);
}
}
}
以上就是完整的代码,试试吧!!!