2016-1-8 Quartz框架的学习,多个气球上升的小动画

//
// BallonView.m
// 气球上升的动画
//
// Created by Mac on 16/1/8.
// Copyright © 2016年 Mac. All rights reserved.
// #import "BallonView.h"
@interface BallonView() @property (nonatomic, assign) NSString *postions;
@property (nonatomic, strong) CADisplayLink *link;
@property (nonatomic, strong) NSMutableArray *locations;
@property (nonatomic, strong) NSMutableArray *ballons; @end
@implementation BallonView
//懒加载 存储位置
- (NSMutableArray *)locations
{
if (!_locations) {
_locations = [NSMutableArray array];
}
return _locations;
}
- (NSMutableArray *)ballons
{
if (!_ballons) {
_ballons = [NSMutableArray array];
NSInteger count = ;
UIImage *ballonImage =[UIImage imageNamed:@"sandyBalloon"];
for (int i = ; i < count; i ++) {
CGFloat left = ;
CGPoint location = CGPointMake(left*(i+), self.frame.size.height - );
// [ballonImage drawAtPoint:location];
[self.locations addObject:[NSValue valueWithCGPoint:location]];
[_ballons addObject:ballonImage];
}
}
return _ballons;
} // Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
NSInteger ballonsOfCount = self.ballons.count;
for (int i = ; i < ballonsOfCount; i ++) {
// 获取气球的位置
CGPoint postion = [self.locations[i] CGPointValue];
postion.y -= arc4random_uniform() * 0.7;
if (postion.y == ) {
postion.y = rect.size.height;
} [self.locations replaceObjectAtIndex:i withObject:[NSValue valueWithCGPoint:postion]];
[self.ballons[i] drawAtPoint:postion];
}
NSLog(@"%s",__func__); } // ------------------------------------------------------------------------------------------------------------------
// Drawing code - (instancetype)init
{
if (self = [super init]) {
[self addAnimition];
}
return self;
}
- (void) addAnimition
{
// [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(setNeedsDisplay) userInfo:nil repeats:YES];
CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(setNeedsDisplay)];
[link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
self.link = link;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.link invalidate];
} @end

2016-1-8 Quartz框架的学习,多个气球上升的小动画

上一篇:Java中的String、StringBuffer和StringBuilder的区别


下一篇:ubuntu apt-get update失败 解决方法