IOS UI transform 属性

本节重点:


理解 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    需要执行的代码

效果图:

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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值