// ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
// ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
@end
// VCView.h
#import <UIKit/UIKit.h>
@interface VCView : UIView
@end
// VCView.m
#import "VCView.h"
@implementation VCView
- (void)awakeFromNib {
// 添加定時(shí)器
// [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(update) userInfo:nil repeats:YES];
// 什么時(shí)候調(diào)用指定的方法?
// 當(dāng)下一次屏幕刷新時(shí)調(diào)用(屏幕每一秒刷新60)
CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
// 想要讓CADisplayLink工作, 必須得要添加到主運(yùn)行循環(huán)當(dāng)中.
[link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
// setNeedsDisplay底層會調(diào)用drawRect,并不是立馬調(diào)用的.只是設(shè)了一個(gè)調(diào)用的標(biāo)志.
// 等下一次屏幕刷新時(shí)才去調(diào)用drawRect
}
static int _snowY = 0;
- (void)update {
NSLog(@"%s",__func__);
_snowY += 10;
if (_snowY > self.bounds.size.height) {
_snowY = 0;
}
// 重繪
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
//加載圖片
UIImage *image = [UIImage imageNamed:@"雪花"];
[image drawAtPoint:CGPointMake(0, _snowY)];
}
@end
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者