//
// 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 =
5;
UIImage *ballonImage =[UIImage imageNamed:
@"sandyBalloon"];
for (
int i =
0; i < count; i ++
) {
CGFloat left =
70;
CGPoint location = CGPointMake(left*(i+
1), self.frame.size.height -
100);
// [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 =
0; i < ballonsOfCount; i ++
) {
// 获取气球的位置
CGPoint postion =
[self.locations[i] CGPointValue];
postion.y -= arc4random_uniform(
10) *
0.7;
if (postion.y ==
0) {
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
转载于:https://www.cnblogs.com/BJTUzhengli/p/5114699.html
相关资源:JAVA上百实例源码以及开源项目