您的位置:首页 > 产品设计 > UI/UE

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息