iOS核心動(dòng)畫之圖片旋轉(zhuǎn)、脈沖動(dòng)畫、水波紋動(dòng)畫

下邊有整體效果,希望能幫助到你!

定義一個(gè)視圖

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

一、圖片旋轉(zhuǎn)三種方式:

第一種:根據(jù)CGPathAddArc 繪畫圖片旋轉(zhuǎn)路線:

/*

?? ? 1、<#CGMutablePathRef? _Nullable path#> 路線

?? ? 2、確定圓心<#CGFloat x#> <#CGFloat y#>

?? ? 3、半徑<#CGFloat radius#>

?? ? 4、起點(diǎn) <#CGFloat startAngle#> 結(jié)束 <#CGFloat endAngle#>

? ? */

?CGPathAddArc(path, NULL, self.view.center.x, self.view.center.y, 0.1, 0, M_PI *2, 1);

? ? CAKeyframeAnimation * frameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

? ? frameAnimation.path= path;

? ? CGPathRelease(path);

? ? frameAnimation.delegate=self;

? ? frameAnimation.duration=10;// 持續(xù)時(shí)間

? ? frameAnimation.repeatCount = -1;// 重復(fù)次數(shù) 如果為0表示不執(zhí)行,-1表示不限制次數(shù),默認(rèn)為0

? ? frameAnimation.autoreverses=NO;

? ? frameAnimation.rotationMode = kCAAnimationRotateAuto;// 樣式

? ? frameAnimation.fillMode = kCAFillModeForwards;

? ? [self.imageView.layeraddAnimation:frameAnimationforKey:nil];


第二種:

?[UIView animateWithDuration:20.0f animations:^{

? ? ? ? if (self.imageView) {

? ? ? ? ? ?self.imageView.transform = CGAffineTransformMakeRotation(M_PI*5);

? ? ?????}

? ?}];


第三種:

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

? ? //默認(rèn)是順時(shí)針效果,若將fromValue和toValue的值互換,則為逆時(shí)針效果

? ? animation.fromValue = [NSNumber numberWithFloat:0.f];

? ? animation.toValue = [NSNumber numberWithFloat: M_PI *2];

? ? animation.duration=30;

? ? animation.autoreverses=NO;

? ? animation.fillMode = kCAFillModeForwards;

? ? animation.repeatCount = MAXFLOAT; //如果這里想設(shè)置成一直自旋轉(zhuǎn),可以設(shè)置為MAXFLOAT,否則設(shè)置具體的數(shù)值則代表執(zhí)行多少次

? ? [self.imageView.layer addAnimation:animation forKey:nil];


持續(xù)旋轉(zhuǎn):

@property(nonatomic,assign) double angle;

?CGAffineTransform endAngle = CGAffineTransformMakeRotation(self.angle * (M_PI / 180.0f));


? ? [UIView animateWithDuration:0.01 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{

? ? ? ? self.imageView.transform= endAngle;

? ? }completion:^(BOOLfinished) {

? ? ? ? self.angle+=10;

? ? ? ? [self startAnimation2];// 上邊任意一種方法回調(diào)

? ? }];

// 當(dāng)視圖停止轉(zhuǎn)動(dòng)時(shí)調(diào)用此方法重新轉(zhuǎn)動(dòng)

-(void)endAnimation {

? ? self.angle+=4;

? ? [self startAnimation2];

}

二、水波紋動(dòng)畫

屬性定義:幾個(gè)波紋定義幾個(gè)X 寬度可以用一個(gè) 也可以分開定義

? ?@property (weak, nonatomic) IBOutlet UIView *backView;

@property(nonatomic,strong) CAShapeLayer * waterLayer1;

@property(nonatomic,strong) CAShapeLayer * waterLayer2;

@property(nonatomic,assign) CGFloat x;

@property(nonatomic,assign) CGFloat y;

@property(nonatomic,assign) CGFloat waveHeight;

@property(nonatomic,assign) CGFloat waveWidth;

@property(nonatomic,assign) int speedWave;

@property(nonatomic,assign) CGFloat waveAmplitude;

@property(nonatomic,assign) int speed;

@property(nonatomic,assign) CGFloat speed_H;

@property(nonatomic,assign) CGFloat offsetXT;


-(instancetype)init {// 給個(gè)初始值,下邊被除數(shù)不能為0

????if (self == [super init]) {

?????????self.speedWave = 3;

? ? ????self.waveAmplitude = 3;

? ? ????self.speed=3;

? ? ????self.waveWidth = self.backView.frame.size.width;

? ? ????self.waveHeight = self.backView.frame.size.height;

? ? ????self.speed_H = self.backView.frame.size.height-20;

????}

return self;

}

-(void)waterAnimation {

//? ? CGFloat y = _waveHeight*sinf(2.5*M_PI*i/_waveWidth + 3*_offset*M_PI/_waveWidth + M_PI/4) + _h;

? ? self.waterLayer1 = [CAShapeLayer layer];

? ? self.waterLayer1.fillColor = [UIColor yellowColor].CGColor;

? ? [self.backView.layer addSublayer:self.waterLayer1];

? ? self.waterLayer2 = [CAShapeLayer layer];

?? ? self.waterLayer2.fillColor = [UIColor redColor].CGColor;

? ? [self.backView.layer addSublayer: self.waterLayer2];

? ? //創(chuàng)建一個(gè)新的 CADisplayLink 對(duì)象,把它添加到一個(gè)runloop中,并給它提供一個(gè) target 和selector 在屏幕刷新的時(shí)候調(diào)用

? ? //CADispayLink相當(dāng)于一個(gè)定時(shí)器 會(huì)一直繪制曲線波紋 看似在運(yùn)動(dòng),其實(shí)是一直在繪畫不同位置點(diǎn)的余弦函數(shù)曲線

? ? CADisplayLink * waveDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(getCurrentWave)];

? ? [waveDisplayLinkaddToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];

}

-(void)getCurrentWave {

? ? // x位置

? ? self.x+=self.speed;

? ? //聲明第一條波曲線的路徑

? ? CGMutablePathRef path = CGPathCreateMutable();

? ? //設(shè)置起始點(diǎn)

? ? CGPathMoveToPoint(path,nil,0,self.waveHeight);

? ? CGFloaty =0.f;

? ? //第一個(gè)波紋的公式

? ? for(floatx =0.f; x <=self.waveWidth; x++) {

? ? ? ? y =self.waveAmplitude*sin((200/self.waveWidth) * (x *M_PI/70) -self.x*M_PI/170) +self.speed_H*1;

? ? ? ? CGPathAddLineToPoint(path,nil, x, y);

? ? ? ? x++;

? ? }

? ? //把繪圖信息添加到路徑里

? ? CGPathAddLineToPoint(path, nil, self.waveWidth, self.backView.frame.size.height);

? ? CGPathAddLineToPoint(path, nil, 0, self.backView.frame.size.height);

? ? //結(jié)束繪圖信息

? ? CGPathCloseSubpath(path);

? ? self.waterLayer1.path= path;

? ? //釋放繪圖路徑

? ? CGPathRelease(path);

? ? [self? ? X2];

}

/// 第二條水波

-(void)X2 {

? ? self.offsetXT += self.speedWave;

? ? CGMutablePathRef pathT = CGPathCreateMutable();

? ? CGPathMoveToPoint(pathT,nil,0,self.waveHeight+50);

? ? CGFloatyT =0.f;

? ? for(floatx =0.f; x <=self.waveWidth; x++) {

? ? ? ? yT =self.waveAmplitude*1.6*sin((200/self.waveWidth) * (x *M_PI/100) -self.offsetXT*M_PI/170) +self.waveHeight;

? ? ? ? CGPathAddLineToPoint(pathT,nil, x, yT-10);

? ? }

? ? CGPathAddLineToPoint(pathT, nil, self.waveWidth, self.backView.frame.size.height);

? ? CGPathAddLineToPoint(pathT, nil, 0, self.backView.frame.size.height);

? ? CGPathCloseSubpath(pathT);

? ? self.waterLayer2.path= pathT;

? ? CGPathRelease(pathT);

}

三、脈沖效果動(dòng)畫

@property (weak, nonatomic) IBOutlet UIView *pulseView;

@property(nonatomic,strong) CAShapeLayer * pulseLayer;

-(void)pulseAnimation {

? ? CGFloat width = self.pulseView.bounds.size.width;

? ? self.pulseLayer = [CAShapeLayer layer];

? ? self.pulseLayer.bounds=CGRectMake(0,0, width, width);

? ? self.pulseLayer.position=CGPointMake(width/2, width/2);

? ? self.pulseLayer.backgroundColor = [UIColor clearColor].CGColor;

? ? self.pulseLayer.path = [UIBezierPath bezierPathWithOvalInRect:self.pulseLayer.bounds].CGPath;

? ? self.pulseLayer.fillColor = [UIColor colorWithRed: 0.3490196078 green:0.737254902 blue:0.8039215686 alpha:1].CGColor;

? ? self.pulseLayer.opacity = 0.0;


? ? CAReplicatorLayer * replicatorLayer = [CAReplicatorLayer layer];

? ? replicatorLayer.bounds=CGRectMake(0,0, width, width);

? ? replicatorLayer.position=CGPointMake(width/2, width/2);

? ? replicatorLayer.instanceCount=4;// 復(fù)制層

? ? replicatorLayer.instanceDelay=1;/// 頻率

? ? [replicatorLayeraddSublayer:self.pulseLayer];


? ? [self.pulseView.layeraddSublayer:replicatorLayer];

? ? [self.pulseView.layerinsertSublayer:replicatorLayeratIndex:0];

}

-(void)startPulseAnimation {

? ? CABasicAnimation * opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];

? ? opacityAnimation.fromValue=@20;// 起始值 (strong 修飾的id值)

? ? opacityAnimation.toValue=@30;// 結(jié)束值(strong 修飾的id值)


? ? CABasicAnimation * scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];

? ? scaleAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DIdentity, 0.0, 0.0, 0.0)];

? ? scaleAnimation.toValue =[NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DIdentity, 1.0, 1.0, 1.0)];


? ? CAAnimationGroup * groupAnimation = [CAAnimationGroup animation];

? ? groupAnimation.animations=@[opacityAnimation, scaleAnimation];

? ? groupAnimation.duration=20;

? ? groupAnimation.autoreverses=NO;

? ? groupAnimation.repeatCount=HUGE;

? ? [self.pulseLayeraddAnimation:groupAnimationforKey:nil];

}


在此附上效果:

聽說有好得三方庫,我還沒有去找過,歡迎各位大佬推薦一個(gè)優(yōu)質(zhì)的三方。。。。。

喜歡的朋友點(diǎn)個(gè)贊唄!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容