图片移动/放大/缩小(frame,center,bonds等方法
2016-01-10 本文已影响539人
AlexPei
bounds方法设置图片大小注意事项
bounds是结构体CGRect需要用CGRect接收而不能用CGSize接收,否则报错:
/Users/peibobo/Desktop/未命名文件夹/buttonAction/buttonAction/ViewController.m:97:12:
Initializing 'CGSize' (aka 'struct CGSize') with an expression of incompatible
type 'CGRect' (aka 'struct CGRect')
//bouneds方式设置放大动画
CGRect bounds = self.btnIcon.bounds;
if (sender.tag == 50) {
bounds.size.height += 100;
bounds.size.width += 100;
[UIView animateWithDuration:1.2 animations:^{
self.btnIcon.bounds = bounds;
}];
}else if(sender.tag == 60){
bounds.size.height -= 100;
bounds.size.width -=100;
//传统方式缩小动画
//开启动画
[UIView beginAnimations:nil context:nil];
//1.设置动画时间
[UIView setAnimationDuration:1.2];
//2.执行动画代码包含进去
self.btnIcon.bounds = bounds;
//3.呈现动画
[UIView commitAnimations];
}
}
center移动图片以图片中心点为移动中心开始移动,用CGPoint接收
- (IBAction)changeIcon:(UIButton *)sender {
CGPoint originalIcon = self.btnIcon.center;
//tag 10 20 30 40 分别对应上 右 下 左
switch (sender.tag) {
case 10:
originalIcon.y -= 100;
self.btnIcon.center = originalIcon;
break;
case 20:
originalIcon.x += 100;
self.btnIcon.center = originalIcon;
break;
case 30:
originalIcon.y += 100;
self.btnIcon.center = originalIcon;
break;
case 40:
originalIcon.x -= 100;
self.btnIcon.center = originalIcon;
break;
default:
break;
}
frame方式移动以及缩放图片大小(无动画)
- (IBAction)changeIcon:(UIButton *)sender {
//获取frame--图片位置CGPoint以及尺寸CGSize
CGRect originalIcon = self.btnIcon.frame;
//switch...case...选择执行的动作
//给self.btnIcon.frame从新赋值
switch (sender.tag) {
case 10:
originalIcon.origin.y -=100;
self.btnIcon.frame = originalIcon;
break;
case 20:
originalIcon.origin.x +=100;
self.btnIcon.frame = originalIcon;
break;
case 30:
originalIcon.origin.y +=100;
self.btnIcon.frame = originalIcon;
break;
case 40:
originalIcon.origin.x -=100;
self.btnIcon.frame = originalIcon;
break;
case 50:
originalIcon.size.width +=100;
originalIcon.size.height +=100;
self.btnIcon.frame = originalIcon;
break;
case 60:
originalIcon.size.height -=100;
originalIcon.size.width -=100;
self.btnIcon.frame = originalIcon;
break;
default:
break;
}
}