UIImageView简单操做

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //======================高亮==============
    _image=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2"] highlightedImage:[UIImage imageNamed:@"3"]];//高亮状态时切换成第二个图片
    _image.frame=CGRectMake(20, 30, 100, 100);//设置图片大小
    
    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(130, 90, 80, 40)];
    //设置按钮的显示名称,
    [button setTitle:@"高亮切换" forState:UIControlStateNormal];
    button.backgroundColor=[UIColor redColor];//设置背景颜色
    [button addTarget:self action:@selector(gaoliang) forControlEvents:UIControlEventTouchUpInside];//给按钮添加事件
    [self.view addSubview:button];
    [self.view addSubview:_image];
    
    //=============给图片添加手势================
    //实例化的时候直接添加图片
    _image1=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1"]];
    _image1.frame=CGRectMake(220, 30, 100, 100);
    [self.view addSubview:_image1];
    //设置图片支持交互
    _image1.userInteractionEnabled=YES;
    //定义手势
    UITapGestureRecognizer *gestur=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(shoushi)];
    //将图片添加到手势
    [_image1 addGestureRecognizer:gestur];
    
    //==============利用Frame控制缩放=================
    _image2=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"4"]];
    _image2.frame=CGRectMake(120, 150, 100, 100);
    [self.view addSubview:_image2];
    UIButton *suoxiao=[[UIButton alloc]initWithFrame:CGRectMake(80, 260, 60, 40) ];
    [suoxiao setTitle:@"缩小" forState:UIControlStateNormal];
    [suoxiao addTarget:self action:@selector(suoxiao) forControlEvents:UIControlEventTouchUpInside];
    suoxiao.backgroundColor=[UIColor redColor];
    UIButton *fangda=[[UIButton alloc]initWithFrame:CGRectMake(180, 260, 60, 40)];
    [fangda setTitle:@"放大" forState:UIControlStateNormal];
    [fangda addTarget:self action:@selector(fangda) forControlEvents:UIControlEventTouchUpInside];
    fangda.backgroundColor=[UIColor redColor];
    [self.view addSubview:suoxiao];
    [self.view addSubview:fangda];
}
-(void)suoxiao{
    _image2.transform=CGAffineTransformMakeScale(0.5, 0.5);
}
-(void)fangda{
    _image2.transform=CGAffineTransformMakeScale(2, 5);
}
//手势方法
-(void)shoushi{
    _image2.transform=CGAffineTransformMakeScale(1, 1);
    NSLog(@"图片手势方法");
}
//高亮方法
-(void)gaoliang{
    if (_image.highlighted==YES) {
        _image.highlighted=NO;
    }else{
        _image.highlighted=YES;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
相关文章
相关标签/搜索