首页 > 其他 > 详细

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

时间:2016-01-08 22:09:12      阅读:185      评论:0      收藏:0      [点我收藏+]
//
//  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

技术分享

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

原文:http://www.cnblogs.com/BJTUzhengli/p/5114699.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!