![](https://img.haomeiwen.com/i1684093/dfd89fa3e8c9ac0c.png)
百度云地址:https://pan.baidu.com/s/1_Yv8iKpAcjclezqlpP5DaQ
视频:http://www.iqiyi.com/w_19s053xjk9.html
//
// ViewController.h
// testRL
//
// Created by yanjinlin on 2018/7/12.
// Copyright © 2018年 yanjinlin. All rights reserved.
//
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger, AnimaType) {
AnimaTypecenter = 0,
AnimaTypeTop = 1,
AnimaTypeDown = 2,
};
typedef NS_ENUM(NSInteger, AnimaXY) {
AnimaXY_X = 0,
AnimaXY_Y = 1,
};
@interface ViewController : UIViewController
@end
//
// ViewController.m
// testRL
//
// Created by yanjinlin on 2018/7/12.
// Copyright © 2018年 yanjinlin. All rights reserved.
//
#import "ViewController.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
@interface ViewController ()<CAAnimationDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//X漂移
UIImageView *backView1=[[UIImageView alloc]initWithFrame:CGRectMake(0, 100, 40, 40)];
[self.view addSubview:backView1];
backView1.image=[UIImage imageNamed:@"girl.jpg"];
[self AnimaRepeatCount:0 AnimaXY:AnimaXY_X Formvalue:0 Tovalue:SCREEN_WIDTH animaView:backView1];
//定点左右摇摆
UIImageView *backView2=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 100)];
backView2.center = self.view.center;
[self.view addSubview:backView2];
backView2.image=[UIImage imageNamed:@"girl.jpg"];
[self AnimaRepeatCount:0 animatype:AnimaTypeTop animaduration:1.0 animaView:backView2];
//Y漂移
UIImageView *backView3=[[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2, 0, 40, 40)];
[self.view addSubview:backView3];
backView3.image=[UIImage imageNamed:@"girl.jpg"];
[self AnimaRepeatCount:0 AnimaXY:AnimaXY_Y Formvalue:0 Tovalue:SCREEN_HEIGHT animaView:backView3];
}
-(void)AnimaRepeatCount:(NSInteger)count AnimaXY:(AnimaXY)animaxy Formvalue:(NSInteger)formvalue Tovalue:(NSInteger)tovalue animaView:(UIView*)View{
CABasicAnimation *Animation= [CABasicAnimation animation]; //定义动画
//左右漂移
if (animaxy ==0) {
Animation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
}else{
Animation=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
}
Animation.fromValue = [NSNumber numberWithFloat:formvalue];
Animation.toValue = [NSNumber numberWithFloat:tovalue];
Animation.duration = 1;//动画持续时间
if (count == 0) {
Animation.repeatCount = MAXFLOAT;//动画重复次数
}else{
Animation.repeatCount = count;//动画重复次数
}
Animation.autoreverses = YES;//是否自动重复
[View.layer addAnimation:Animation forKey:@"animateLayer"];
}
-(void)AnimaRepeatCount:(NSInteger)count animatype:(AnimaType)type animaduration:(float)duration animaView:(UIView*)View{
CABasicAnimation *baseAnimation = [CABasicAnimation animation];
//动画运动的方式,现在指定的是围绕Z轴旋转
baseAnimation.keyPath = @"transform.rotation.z";
//动画持续时间
baseAnimation.duration = duration;
//开始的角度
baseAnimation.fromValue = [NSNumber numberWithFloat:-M_PI_4];
//结束的角度
baseAnimation.toValue = [NSNumber numberWithFloat:M_PI_4];
//动画的运动方式
baseAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
//是否反向移动动画
baseAnimation.autoreverses = YES;
//动画重复次数
if (count==0) {
baseAnimation.repeatCount = MAXFLOAT;
}else{
baseAnimation.repeatCount = count;
}
//动画的代理
baseAnimation.delegate = self;
//动画结束后的状态
baseAnimation.fillMode = kCAFillModeForwards;
[baseAnimation setValue:@"RightandLeft" forKey:@"Animation"];
[View.layer addAnimation:baseAnimation forKey:@"Animation"];
switch (type) {
case 0:{
View.layer.anchorPoint = CGPointMake(0.5, 0.5);//中间摆动
}
break;
case 1:{
View.layer.anchorPoint = CGPointMake(0.5, 1);//上面摆动
}
break;
default:{
View.layer.anchorPoint = CGPointMake(0.5, 0);//下面摆动
}
break;
}
}
@end
网友评论