首次编辑 | 2024/10/25 09:00 | |
修改1 | 2024/10/25 13:08 | 增加objective-c版 |
在 MacOS 下用 objective-c,swift 语言编写的一个图形图像处理小程序,属性动画。
操作系统平台:MacOS
IDE:xcode
编程语言:objective-c,swift
一:属性动画小程序
先看一下运行视频效果
objective-c版
图形图像处理小程序-属性动画(objective-c版)
swift版
图形图像处理小程序-属性动画(swift版)
小程序主要动画效果是绕X轴旋转,绕Y轴旋转,绕Z轴旋转,再放大缩小。
二:属性动画小程序效果图
objective-c版
swift版
三:部分源代码
objective-c版
小程序界面设计源代码:
self.view.layer.cornerRadius=10;
self.view.layer.backgroundColor=[UIColor cyanColor].CGColor;
layer1=[[CALayer alloc]init];
layer1.backgroundColor=[UIColor yellowColor].CGColor;
layer1.frame=CGRectMake(100,50,100,100);
[self.view.layer addSublayer:layer1];
layer1.borderWidth=5;
layer1.borderColor=[UIColor blueColor].CGColor;
label1=[[UILabel alloc] initWithFrame:CGRectMake(100,160,200,30)];
[label1 setBackgroundColor:[UIColor redColor]];
label1.text=@"属性动画Objective-C";
[self.view addSubview:label1];
UIButton* bn1=[UIButton buttonWithType:UIButtonTypeSystem];
bn1.frame=CGRectMake(100,220,100,30);
[bn1 setBackgroundColor:[UIColor whiteColor]];
[bn1 setTitle:@"xRotate" forState:UIControlStateNormal];
[bn1 addTarget:self action:@selector(xRotate:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bn1];
UIButton* bn2=[UIButton buttonWithType:UIButtonTypeSystem];
bn2.frame=CGRectMake(100,255,100,30);
[bn2 setBackgroundColor:[UIColor whiteColor]];
[bn2 setTitle:@"yRotate" forState:UIControlStateNormal];
[bn2 addTarget:self action:@selector(yRotate:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bn2];
UIButton* bn3=[UIButton buttonWithType:UIButtonTypeSystem];
bn3.frame=CGRectMake(100,290,100,30);
[bn3 setBackgroundColor:[UIColor whiteColor]];
[bn3 setTitle:@"zRotate" forState:UIControlStateNormal];
[bn3 addTarget:self action:@selector(zRotate:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bn3];
UIButton* bn4=[UIButton buttonWithType:UIButtonTypeSystem];
bn4.frame=CGRectMake(100,340,100,30);
[bn4 setBackgroundColor:[UIColor whiteColor]];
[bn4 setTitle:@"scale" forState:UIControlStateNormal];
[bn4 addTarget:self action:@selector(scale:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bn4];
绕X轴旋转:
-(void) xRotate:(id)sender{
CABasicAnimation* anim=[CABasicAnimation animationWithKeyPath:@"transform"];
CATransform3D fromValue =layer1.transform;
// 设置动画开始时的属性值
anim.fromValue = [NSValue valueWithCATransform3D:fromValue];
// 绕X轴旋转180°
CATransform3D toValue = CATransform3DRotate(fromValue, M_PI , 1 , 0 , 0);
anim.toValue = [NSValue valueWithCATransform3D:toValue]; // 设置动画结束时的属性值
label1.text=@"绕X轴旋转";
anim.duration = 10;
layer1.transform = toValue;
anim.removedOnCompletion = YES;
[layer1 addAnimation:anim forKey:nil];
}
绕Y轴旋转:
-(void) yRotate:(id)sender{
// 绕Y轴旋转180°
CABasicAnimation* anim=[CABasicAnimation animationWithKeyPath:@"transform"];
CATransform3D fromValue =layer1.transform;
anim.fromValue = [NSValue valueWithCATransform3D:fromValue];
CATransform3D toValue = CATransform3DRotate(fromValue, M_PI , 0 , 1 , 0);
anim.toValue = [NSValue valueWithCATransform3D:toValue]; // 设置动画结束时的属性值
label1.text=@"绕Y轴旋转";
anim.duration = 10;
layer1.transform = toValue;
anim.removedOnCompletion = YES;
[layer1 addAnimation:anim forKey:nil];
}
绕Z轴旋转:
-(void)zRotate:(id)sender{
// 绕Z轴旋转180°
CABasicAnimation* anim=[CABasicAnimation animationWithKeyPath:@"transform"];
CATransform3D fromValue =layer1.transform;
anim.fromValue = [NSValue valueWithCATransform3D:fromValue];
CATransform3D toValue = CATransform3DRotate(fromValue, M_PI , 0 , 0 , 1);
anim.toValue = [NSValue valueWithCATransform3D:toValue]; // 设置动画结束时的属性值
label1.text=@"绕Z轴旋转";
anim.duration = 10;
layer1.transform = toValue;
anim.removedOnCompletion = YES;
[layer1 addAnimation:anim forKey:nil];
}
放大缩小:
-(void)scale:(id)sender{
//放大缩小
CAKeyframeAnimation* anim=[CAKeyframeAnimation animationWithKeyPath:@"transform"];
anim.values=@[[NSValue valueWithCATransform3D:layer1.transform],
[NSValue valueWithCATransform3D:CATransform3DScale (layer1.transform,0.2,0.2,1)],
[NSValue valueWithCATransform3D:CATransform3DScale (layer1.transform,1.5,1.5,1)],
[NSValue valueWithCATransform3D:CATransform3DScale (layer1.transform,0.2,0.2,1)],
[NSValue valueWithCATransform3D:layer1.transform]];
label1.text=@"放大缩小";
anim.duration=10;
anim.removedOnCompletion = YES;
[layer1 addAnimation:anim forKey:nil];
}
swift版
小程序界面设计源代码:
layer1=CALayer()
layer1.borderWidth=3
layer1.borderColor=UIColor.cyan.cgColor
layer1.backgroundColor=UIColor.yellow.cgColor
layer1.frame = CGRect(x:100,y:50,width:100,height:100)
self.view.layer.addSublayer(layer1)
let bnArray=["xRotate","yRotate","zRotate","scale"]
let selectors:[Selector]=[#selector(xRotate),#selector(yRotate),#selector(zRotate),#selector(scale)]
label1=UILabel()
label1.frame = CGRect(x:100,y:160,width:200,height:25)
label1.text="属性动画swift版"
label1.backgroundColor=UIColor.red
self.view.addSubview(label1)
for i in 0 ..< 4 {
let bn=UIButton(type:.system)
bn.frame = CGRect(x:100,y:200+i*30,width:100,height:25)
bn.setTitle(bnArray[i],for:.normal)
bn.backgroundColor=UIColor.cyan
self.view.addSubview(bn)
bn.addTarget(self,action:selectors[i],for: .touchUpInside)
}
绕X轴旋转:
func xRotate(sender:AnyObject){
let anim=CABasicAnimation(keyPath: "transform")
let fromValue=layer1.transform
anim.fromValue=NSValue(caTransform3D:fromValue)
let toValue=CATransform3DRotate(fromValue,CGFloat(M_PI),1,0,0)
anim.toValue=NSValue(caTransform3D:toValue)
anim.duration=10
layer1.transform=toValue
anim.isRemovedOnCompletion=true
label1.text="绕X轴旋转"
layer1.add(anim,forKey:nil)
}
绕Y轴旋转:
func yRotate(sender:AnyObject){
let anim=CABasicAnimation(keyPath: "transform")
let fromValue=layer1.transform
anim.fromValue=NSValue(caTransform3D:fromValue)
let toValue=CATransform3DRotate(fromValue,CGFloat(M_PI),0,1,0)
anim.toValue=NSValue(caTransform3D:toValue)
anim.duration=10
layer1.transform=toValue
anim.isRemovedOnCompletion=true
label1.text="绕Y轴旋转"
layer1.add(anim,forKey:nil)
}
绕Z轴旋转:
func zRotate(sender:AnyObject){
let anim=CABasicAnimation(keyPath: "transform")
let fromValue=layer1.transform
anim.fromValue=NSValue(caTransform3D:fromValue)
let toValue=CATransform3DRotate(fromValue,CGFloat(M_PI),0,0,1)
anim.toValue=NSValue(caTransform3D:toValue)
anim.duration=10
layer1.transform=toValue
anim.isRemovedOnCompletion=true
label1.text="绕Z轴旋转"
layer1.add(anim,forKey:nil)
}
放大缩小:
func scale(sender:AnyObject){
let anim = CAKeyframeAnimation(keyPath: "transform")
anim.values=[NSValue(caTransform3D:layer1.transform),
NSValue(caTransform3D:CATransform3DScale (layer1.transform,0.2,0.2,1)),
NSValue(caTransform3D:CATransform3DScale (layer1.transform,1.5,1.5,1)),
NSValue(caTransform3D:CATransform3DScale (layer1.transform,0.2,0.2,1)),
NSValue(caTransform3D:layer1.transform)]
label1.text="放大缩小"
anim.duration=10
anim.isRemovedOnCompletion = true
layer1.add(anim,forKey:nil)
}
以上内容仅供参考,如有不对,欢迎指正。
参考资料:
1.《疯狂ios讲义》(基础篇)李刚编著 中国工信出版集团 电子工业出版
2.CGRectMake(_:_:_:_:) | Apple Developer Documentation
https://developer.apple.com/documentation/coregraphics/1455245-cgrectmake