图形图像处理-属性动画小程序(objective-c,swift)

修改纪录
首次编辑2024/10/25 09:00
修改12024/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

MySQL中文参考手册 译者:晏子 (clyan@sohu.com) 主页:http://linuxdb.yeah.net -------------------------------------------------------------------------------- 第一章,前一章,下一章, 最后一章, 目录. -------------------------------------------------------------------------------- 1 MySQL 的一般信息 这是MySQL参考手册;它记载了MySQL版本3.23.7-alpha。 MySQL 是一个快速、多线程、多用户和强壮的SQL数据库服务器。 对Unix和 OS/2 平台,MySQL基本上是免费的;但对微软平台,你在30 天的试用期后必须获得一个MySQL 许可证。详见第三节 MySQL许可证和技术支持。 MySQL 主页提供有关MySQL的最新信息。 对于MySQL能力的讨论,详见1.4 MySQL 的主要特征。 对于安装指南,见4 安装 MySQL。对于有关移植MySQL到新机器或操作系统的技巧,参见G 对移植到其他系统的说明。 有关从 3.21 版升级的信息,详见4.16.2 从一个 3.21 版本升级到 3.22 。 MySQL的入门教程,见8 MySQL 教程。 SQL和基准信息的例子,见基准目录(在分发中的'sql-bench'目录)。 对于新特征和错误修复一个历史记录,见D MySQL的变迁。 对于当前已知错误和功能缺陷的一张列表,见E MySQL已知错误和设计缺陷。 未来计划,见F 我们想要在未来加入到MySQL 的计划表( TODO )。 这个计划的所有贡献者的名单,见C MySQL 的贡献者。 重要: 将臭虫(错误)报告、问提和建议发到邮件列表(原文未提供)。 对源代码分发,mysqlbug 脚本可在‘scripts’目录下找到。 对二进制的分发,mysqlbug可在‘bin’目录下找到。 如果你有任何关于这本手册的增补或修正的任何建议,请将它们发给手册小组(docs@mysql.com )。 1.1 什么是 MySQLMySQL是一个真正的多用户、多线程SQL数据库服务器。SQL(结构化查询语言)是世界上最流行的和标准化的数据库语言。MySQL是以一个客户机/服务器结构的实现,它由一个服务器守护程序mysqld和很多不同的客户程序和库组成。 SQL是一种标准化的语言,它使得存储、更新和存取信息更容易。例如,你能用SQL语言为一个网站检索产品信息及存储顾客信息,同时MySQL也足够快和灵活以允许你存储记录文件和图像。 MySQL 主要目标是快速、健壮和易用。最初是因为我们需要这样一个SQL服务器,它能处理与任何可不昂贵硬件平台上提供数据库的厂家在一个数量级上的大型数据库,但速度更快,MySQL就开发出来。自1996年以来,我们一直都在使用MySQL,其环境有超过 40 个数据库,包含 10,000个表,其中500多个表超过7百万行,这大约有100 个吉字节(GB)的关键应用数据。 MySQL建立的基础是业已用在高要求的生产环境多年的一套实用例程。尽管MySQL仍在开发中,但它已经提供一个丰富和极其有用的功能集。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值