IOS UI transform 属性
2015-01-30 17:46
393 查看
本节重点:
理解 transform 属性
. 通过 transform 属性 可以对控件进行位移 缩放 扩大 旋转 的操作
CGAffineTransform CGAffineTransformScale(CGAffineTransform t, CGFloat sx, CGFloat sy) 缩放效果
CGAffineTransform CGAffineTransformRotate(CGAffineTransform t, CGFloat angle) 旋转效果
动画
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations
ios动画效果之一 NSTimeInterval :动画持续时间 animations 需要执行的代码
效果图:
素材打包:
链接:http://pan.baidu.com/s/1gdlFcAn 密码:yp54
代码示例:
//
// SJViewController.m
// 03.tranfromDemo
//
// Created by SJ.abnormal on 15-1-29.
// Copyright (c) 2015年 SJ.abnormal. All rights reserved.
//
#import "SJViewController.h"
typedef enum { //设置tag常量值
kOne = 1,
kTwo,
kThree,
kFour,
kFive,
kSix,
kSeven,
kEight,
kNine
} VALUE_Tag;
@interface SJViewController ()
@end
@implementation SJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self imageViewWithFrame:CGRectMake(100, 100, 100, 100) andTag:kFive andBackgroundImage:@"image"];
[self buttonWithFrame:CGRectMake(20, 350, 35, 35) andTag:kFour andImageNormal:@"left_normal" andImageHeighted:@"left_highlighted"];
[self buttonWithFrame:CGRectMake(90, 350, 35, 35) andTag:kSix andImageNormal:@"right_normal" andImageHeighted:@"right_highlighted"];
[self buttonWithFrame:CGRectMake(55, 315, 35, 35) andTag:kEight andImageNormal:@"top_normal" andImageHeighted:@"top_highlighted"];
[self buttonWithFrame:CGRectMake(55, 385, 35, 35) andTag:kTwo andImageNormal:@"bottom_normal" andImageHeighted:@"bottom_highlighted"];
[self buttonWithFrame:CGRectMake(180, 315, 35, 35) andTag:kSeven andImageNormal:@"left_rotate_normal" andImageHeighted:@"left_highlighted"];
[self buttonWithFrame:CGRectMake(240, 315, 35, 35) andTag:kNine andImageNormal:@"right_rotate_normal" andImageHeighted:@"right_rotate_highlighted"];
[self buttonWithFrame:CGRectMake(180, 375, 35, 35) andTag:kOne andImageNormal:@"minus_normal" andImageHeighted:@"minus_highlighted"];
[self buttonWithFrame:CGRectMake(240, 375, 35, 35) andTag:kThree andImageNormal:@"plus_normal" andImageHeighted:@"plus_highlighted"];
}
#pragma mark - imageView
- (void) imageViewWithFrame: (CGRect) frame andTag:(NSUInteger)index andBackgroundImage: (NSString *) image {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:frame]; //设置控件的坐标与大小
imageView.image = [UIImage imageNamed:image]; //设置控件显示的图像
imageView.tag = index; //设置控件tag值
[self.view addSubview:imageView];
}
#pragma mark - Button
- (void) buttonWithFrame:(CGRect) frame andTag:(NSUInteger)index andImageNormal:(NSString *) normal andImageHeighted:(NSString *) highlighted {
UIButton *btn = [[UIButton alloc]initWithFrame:frame]; //设置控件坐标以及大小
btn.tag = index; //设置位置标识值
[btn setBackgroundImage:[UIImage imageNamed:normal] forState:UIControlStateNormal]; //设置默认状态下的图片
[btn setBackgroundImage:[UIImage imageNamed:highlighted] forState:UIControlStateHighlighted]; //设置高亮(被点击)显示的图片
[btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; //添加触发事件
[self.view addSubview:btn]; //添加到主视图上
}
#pragma mark - ButtonClick
- (void) buttonClick:(UIButton *) btn {
int index = btn.tag; // 取出tag值
UIImageView *imageview = (UIImageView *)[self.view viewWithTag:kFive]; //取出指定tag值的子视图
CGPoint center = imageview.center; //取出imageVew的center 因为OC中不允许直接修改对象结构体属性的成员
if (index % 2) {
[self buttonRotation:index andImageView:imageview];
}else {
[self buttonMblie:index andImageView:imageview andFrame:center];
}
}
#pragma mark - buttonMblie isAnimating
- (void) buttonMblie:(NSUInteger) index andImageView:(UIImageView *)imageview andFrame: (CGPoint) center {
switch (index) {
break;
case kTwo:
center.y += 50;
break;
case kFour:
center.x -= 50;
break;
case kSix:
center.x += 50;
break;
case kEight:
center.y -= 50;
break;
default:
break;
}
//动画语句 animateWithDuration 动画持续事件 block语语句块内 是要执行的动漫的代码
[UIView animateWithDuration:2 animations:^{
imageview.center = center;
}];
}
#pragma mark - buttonRotation
- (void) buttonRotation:(NSUInteger) index andImageView:(UIImageView *)imageview {
//动画语句 animateWithDuration 动画持续事件 block语语句块内 是要执行的动漫的
[UIView animateWithDuration:2 animations:^{
switch (index) {
break;
case kOne:
imageview.transform =CGAffineTransformScale(imageview.transform, 0.9, 0.9);
break;
case kThree:
imageview.transform =CGAffineTransformScale(imageview.transform, 1.5, 1.5);
break;
case kSeven:
imageview.transform =CGAffineTransformRotate(imageview.transform, -M_PI_4);
break;
case kNine:
imageview.transform =CGAffineTransformRotate(imageview.transform, M_PI_4);
break;
default:
break;
}
}];
}
@end
理解 transform 属性
. 通过 transform 属性 可以对控件进行位移 缩放 扩大 旋转 的操作
CGAffineTransform CGAffineTransformScale(CGAffineTransform t, CGFloat sx, CGFloat sy) 缩放效果
CGAffineTransform CGAffineTransformRotate(CGAffineTransform t, CGFloat angle) 旋转效果
动画
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations
ios动画效果之一 NSTimeInterval :动画持续时间 animations 需要执行的代码
效果图:
素材打包:
链接:http://pan.baidu.com/s/1gdlFcAn 密码:yp54
代码示例:
//
// SJViewController.m
// 03.tranfromDemo
//
// Created by SJ.abnormal on 15-1-29.
// Copyright (c) 2015年 SJ.abnormal. All rights reserved.
//
#import "SJViewController.h"
typedef enum { //设置tag常量值
kOne = 1,
kTwo,
kThree,
kFour,
kFive,
kSix,
kSeven,
kEight,
kNine
} VALUE_Tag;
@interface SJViewController ()
@end
@implementation SJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self imageViewWithFrame:CGRectMake(100, 100, 100, 100) andTag:kFive andBackgroundImage:@"image"];
[self buttonWithFrame:CGRectMake(20, 350, 35, 35) andTag:kFour andImageNormal:@"left_normal" andImageHeighted:@"left_highlighted"];
[self buttonWithFrame:CGRectMake(90, 350, 35, 35) andTag:kSix andImageNormal:@"right_normal" andImageHeighted:@"right_highlighted"];
[self buttonWithFrame:CGRectMake(55, 315, 35, 35) andTag:kEight andImageNormal:@"top_normal" andImageHeighted:@"top_highlighted"];
[self buttonWithFrame:CGRectMake(55, 385, 35, 35) andTag:kTwo andImageNormal:@"bottom_normal" andImageHeighted:@"bottom_highlighted"];
[self buttonWithFrame:CGRectMake(180, 315, 35, 35) andTag:kSeven andImageNormal:@"left_rotate_normal" andImageHeighted:@"left_highlighted"];
[self buttonWithFrame:CGRectMake(240, 315, 35, 35) andTag:kNine andImageNormal:@"right_rotate_normal" andImageHeighted:@"right_rotate_highlighted"];
[self buttonWithFrame:CGRectMake(180, 375, 35, 35) andTag:kOne andImageNormal:@"minus_normal" andImageHeighted:@"minus_highlighted"];
[self buttonWithFrame:CGRectMake(240, 375, 35, 35) andTag:kThree andImageNormal:@"plus_normal" andImageHeighted:@"plus_highlighted"];
}
#pragma mark - imageView
- (void) imageViewWithFrame: (CGRect) frame andTag:(NSUInteger)index andBackgroundImage: (NSString *) image {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:frame]; //设置控件的坐标与大小
imageView.image = [UIImage imageNamed:image]; //设置控件显示的图像
imageView.tag = index; //设置控件tag值
[self.view addSubview:imageView];
}
#pragma mark - Button
- (void) buttonWithFrame:(CGRect) frame andTag:(NSUInteger)index andImageNormal:(NSString *) normal andImageHeighted:(NSString *) highlighted {
UIButton *btn = [[UIButton alloc]initWithFrame:frame]; //设置控件坐标以及大小
btn.tag = index; //设置位置标识值
[btn setBackgroundImage:[UIImage imageNamed:normal] forState:UIControlStateNormal]; //设置默认状态下的图片
[btn setBackgroundImage:[UIImage imageNamed:highlighted] forState:UIControlStateHighlighted]; //设置高亮(被点击)显示的图片
[btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; //添加触发事件
[self.view addSubview:btn]; //添加到主视图上
}
#pragma mark - ButtonClick
- (void) buttonClick:(UIButton *) btn {
int index = btn.tag; // 取出tag值
UIImageView *imageview = (UIImageView *)[self.view viewWithTag:kFive]; //取出指定tag值的子视图
CGPoint center = imageview.center; //取出imageVew的center 因为OC中不允许直接修改对象结构体属性的成员
if (index % 2) {
[self buttonRotation:index andImageView:imageview];
}else {
[self buttonMblie:index andImageView:imageview andFrame:center];
}
}
#pragma mark - buttonMblie isAnimating
- (void) buttonMblie:(NSUInteger) index andImageView:(UIImageView *)imageview andFrame: (CGPoint) center {
switch (index) {
break;
case kTwo:
center.y += 50;
break;
case kFour:
center.x -= 50;
break;
case kSix:
center.x += 50;
break;
case kEight:
center.y -= 50;
break;
default:
break;
}
//动画语句 animateWithDuration 动画持续事件 block语语句块内 是要执行的动漫的代码
[UIView animateWithDuration:2 animations:^{
imageview.center = center;
}];
}
#pragma mark - buttonRotation
- (void) buttonRotation:(NSUInteger) index andImageView:(UIImageView *)imageview {
//动画语句 animateWithDuration 动画持续事件 block语语句块内 是要执行的动漫的
[UIView animateWithDuration:2 animations:^{
switch (index) {
break;
case kOne:
imageview.transform =CGAffineTransformScale(imageview.transform, 0.9, 0.9);
break;
case kThree:
imageview.transform =CGAffineTransformScale(imageview.transform, 1.5, 1.5);
break;
case kSeven:
imageview.transform =CGAffineTransformRotate(imageview.transform, -M_PI_4);
break;
case kNine:
imageview.transform =CGAffineTransformRotate(imageview.transform, M_PI_4);
break;
default:
break;
}
}];
}
@end
相关文章推荐
- (论坛答疑点滴)如何对动态的对象进行操作,属性付值
- XHTML 事件属性(九)
- XML HTTP Request的属性和方法简介-ASP编程教程
- 对象转换对同属性进行赋值
- vs2008属性
- 关于UIView的autoresizingMask属性
- action attribute和name 属性区别
- HTML中input标签maxlength属性的妙处
- js中常用属性备忘
- C#中的特性 和 通过反射获取属性上的特性
- Apache安装与属性配置
- Android开发中设置系统全局属性方法总结
- Android自定义属性时TypedArray的使用方法
- UITextField常用属性及键盘收回的几种方式
- WebService 之 属性详解
- 数据库属性hibernate.dialect的设置
- Swift set get 属性
- 织梦导航高亮栏目属性样式标签currentstyle调取自定义字段的方法~typelink~
- [微信小程序]给data的对象的属性赋值
- AVCaptureDevice 属性介绍