您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關iOS如何實現波浪效果的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
本文實例為大家分享了iOS實現波浪效果的具體代碼,供大家參考,具體內容如下
代碼:
@interface ViewController () @property (strong, nonatomic) CADisplayLink *displayLink; @property (strong, nonatomic) CAShapeLayer *shapeLayer; @property (strong, nonatomic) UIBezierPath *path; @property (strong, nonatomic) CAShapeLayer *shapeLayer2; @property (strong, nonatomic) UIBezierPath *path3; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _shapeLayer = [CAShapeLayer layer]; _shapeLayer.frame = CGRectMake(0, 100, 375, 150); [self.view.layer addSublayer:_shapeLayer]; _shapeLayer2 = [CAShapeLayer layer]; _shapeLayer2.frame = CGRectMake(0, 100, 375, 150); [self.view.layer addSublayer:_shapeLayer2]; _shapeLayer.fillColor = [[UIColor yellowColor] colorWithAlphaComponent:0.3].CGColor; _shapeLayer2.fillColor = [[UIColor blueColor] colorWithAlphaComponent:0.3].CGColor; _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawPath)]; [_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes]; } - (void)drawPath { static double i = 0; CGFloat A = 10.f;//A振幅 CGFloat k = 0;//y軸偏移 CGFloat ω = 0.03;//角速度ω變大,則波形在X軸上收縮(波形變緊密);角速度ω變小,則波形在X軸上延展(波形變稀疏)。不等于0 CGFloat φ = 0 + i;//初相,x=0時的相位;反映在坐標系上則為圖像的左右移動。 //y=Asin(ωx+φ)+k _path = [UIBezierPath bezierPath]; _path3 = [UIBezierPath bezierPath]; [_path moveToPoint:CGPointZero]; [_path3 moveToPoint:CGPointZero]; for (int i = 0; i < 376; i ++) { CGFloat x = i; CGFloat y = A * sin(ω*x+φ)+k; CGFloat y2 = A * cos(ω*x+φ)+k; [_path addLineToPoint:CGPointMake(x, y)]; [_path3 addLineToPoint:CGPointMake(x, y2)]; } [_path addLineToPoint:CGPointMake(375, -100)]; [_path addLineToPoint:CGPointMake(0, -100)]; _path.lineWidth = 1; _shapeLayer.path = _path.CGPath; [_path3 addLineToPoint:CGPointMake(375, -100)]; [_path3 addLineToPoint:CGPointMake(0, -100)]; _path3.lineWidth = 1; _shapeLayer2.path = _path3.CGPath; i += 0.1; if (i > M_PI * 2) { i = 0;//防止i越界 } }
感謝各位的閱讀!關于“iOS如何實現波浪效果”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。