1.首先:在storyboard里面拖一幅圖片和view 設(shè)置他們的約束,把彈幕view加到view上清空彈幕view的背景色

2.自定義一個view
DanMuView.h

DanMuView.m
#import"DanMuView.h"
@interfaceDanMuView()
@property(nonatomic,assign)CGFloat imageX;
@end
@implementationDanMuView
/**
當(dāng)控件從xib/sb中創(chuàng)建是會調(diào)用
*/
-(void)awakeFromNib
{
self.imageX=375;
[self addTimer];
}
//繪制內(nèi)容到這個view上面
/**
不能直接調(diào)用(系統(tǒng)自動調(diào)用,每次調(diào)用該方法會將上一次繪制的內(nèi)容清空)
*/
- (void)drawRect:(CGRect)rect
{
//加載圖片
UIImage*image = [UIImage imageNamed:@"oscillogram_4@2x"];
//繪制圖片
[image drawAtPoint:CGPointMake(self.imageX,10)];
}
#pragma mark-添加定時器
-(void)addTimer
{
//每秒執(zhí)行60次回調(diào)方法
CADisplayLink*link = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateTimer)];
//將定時器添加到RunLoop
[link addToRunLoop:[NSRunLoop currentRunLoop]forMode:NSRunLoopCommonModes];
}
/**
定時器的回調(diào)方法
*/
-(void)updateTimer{
NSLog(@"%s",__FUNCTION__);
self.imageX-=1;
//通知重新繪制
[self setNeedsDisplay];
}
@end
3.把圖片放入工程當(dāng)中,由于沒有真正想彈幕那種圖片,唯有用藍(lán)色圖片做代替

4.效果圖:

最后!歡迎加入iOS技術(shù)大群,學(xué)習(xí)知識交更多的同行朋友? ? ? ? ? ? QQ群號:580284575