objective-c UIImageView

//

//  ViewController.m

//  UIImageView

//

//  Created by 非凡 程序员 on 15/11/12.

//  Copyright (c) 2015 非凡 程序员. All rights reserved.

//


#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    _i=20;

    //图片实例化方法一:

    UIImageView  * imageView = [ [ UIImageView alloc ] init ] ;

    

    //图片实例化方法二:

    UIImageView  * imageViewI = [ [ UIImageView alloc ] initWithFrame: CGRectMake ( 10, 10, 200, 200 ) ] ;

    

    //设置ImageView的内容

    imageViewI.image = [ UIImage imageNamed:@"0" ] ;

    

    

    //图片实例化方法三:

    UIImageView  * imageViewII = [ [ UIImageView alloc ] initWithImage : [ UIImage imageNamed : @"4" ] ] ;

    

    //设置imageView的大小

    imageViewII.frame = CGRectMake ( 180, 100, 100, 100 ) ;

    

    

    //图片实例化方法四:(图片两个状态)

    _imageViewIII = [ [ UIImageView alloc ] initWithImage : [ UIImage imageNamed:@"1" ] highlightedImage : [ UIImage imageNamed:@"2" ] ] ;

    _imageViewIII.frame = CGRectMake ( 5,5, 100, 100 ) ;

    

    

    //将图片添加到页面上

     [ self.view addSubview:_imageViewIII ] ;

    

    

    //添加按钮控制图片状态

    UIButton  * button = [ [ UIButton alloc ] initWithFrame : CGRectMake ( 80, 80, 180, 30 ) ] ;

    [ button setTitle : @"高亮切换" forState : UIControlStateNormal ] ;

    button.backgroundColor = [ UIColor redColor ] ;

    [ button  addTarget : self action : @selector(gaoLiang) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

    

    

// =========   关于图片的手势    ========

    

    _imageViewIIII = [ [ UIImageView alloc ] initWithImage : [ UIImage imageNamed:@"4" ] ];

    

    _imageViewIIII.frame = CGRectMake(10, 150, 100, 100);

    

    _imageViewIIII.backgroundColor=[UIColor redColor];//可以给图片添加背景颜色明确imageimageView的差别

    

    //将图片添加到页面上

    [self.view addSubview:_imageViewIIII];

    

    //图片支持交互

    _imageViewIIII.userInteractionEnabled = YES;

    

//    定义手势

    UITapGestureRecognizer *gestur = [ [ UITapGestureRecognizer alloc ]initWithTarget:self action:@selector(shouShi) ];

    

//    将图片添加手势

    [_imageViewIIII addGestureRecognizer:gestur];

    

    //  ===========  图片旋转  ============

    _rotationImageView = [[ UIImageView alloc]initWithImage:[ UIImage imageNamed:@"0" ] ] ;

    _rotationImageView.frame=CGRectMake(200, 200, 100, 100);

    [self.view addSubview:_rotationImageView];

    

    //添加按钮控制图片状态

    UIButton  * buttonI = [ [ UIButton alloc ] initWithFrame : CGRectMake ( 200, 150, 80, 30 ) ] ;

    [ buttonI setTitle : @"图片旋转" forState : UIControlStateNormal ] ;

    buttonI.backgroundColor = [ UIColor redColor ] ;

    [ buttonI  addTarget : self action : @selector(xuanZhuan) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:buttonI];

    

    

    //  ===========  图片放缩  ============

    _changeSizeImageView = [[ UIImageView alloc]initWithImage:[ UIImage imageNamed:@"2" ] ] ;

    _changeSizeImageView.frame=CGRectMake(20, 300, 100, 100);

    [self.view addSubview:_changeSizeImageView];

    

    //添加按钮控制图片放大

    UIButton  * buttonBig = [ [ UIButton alloc ] initWithFrame : CGRectMake ( 110, 320, 80, 30 ) ] ;

    [ buttonBig setTitle : @"图片放大" forState : UIControlStateNormal ] ;

    buttonBig.backgroundColor = [ UIColor redColor ] ;

    [ buttonBig  addTarget : self action : @selector(fangDa) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:buttonBig];

    //添加按钮控制图片缩小

    UIButton  * buttonSmall = [ [ UIButton alloc ] initWithFrame : CGRectMake ( 110, 360, 80, 30 ) ] ;

    [ buttonSmall setTitle : @"图片缩小" forState : UIControlStateNormal ] ;

    buttonSmall.backgroundColor = [ UIColor redColor ] ;

    [ buttonSmall  addTarget : self action : @selector(suoXiao) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:buttonSmall];

    

    

    //======  利用Frame控制缩放  ======

    _changeFrameImageView = [[ UIImageView alloc]initWithImage:[ UIImage imageNamed:@"5" ] ] ;

    _changeFrameImageView.frame=CGRectMake(120, 430, 100, 100);

    [self.view addSubview:_changeFrameImageView];

    

    //添加按钮控制图片放大

    UIButton  * frameBig = [ [ UIButton alloc ] initWithFrame : CGRectMake ( 110, 600, 50, 30 ) ] ;

    [ frameBig setTitle : @"放大" forState : UIControlStateNormal ] ;

    frameBig.backgroundColor = [ UIColor redColor ] ;

    [ frameBig  addTarget : self action : @selector(zengDa) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:frameBig];


    //添加按钮控制图片缩小

    UIButton  * frameSmall = [ [ UIButton alloc ] initWithFrame : CGRectMake ( 220, 600,50, 30 ) ] ;

    [ frameSmall setTitle : @"缩小" forState : UIControlStateNormal ] ;

    frameSmall.backgroundColor = [ UIColor redColor ] ;

    [ frameSmall  addTarget : self action : @selector(bianXiao) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:frameSmall];

    

//    _imageViewIII.center=CGPointZero;//设置图片中心位置,CGPointZero是原点(00


//    [self.view bringSubviewToFront:imageViewI];//将控件移至最前方

    

//    _imageViewIIII.contentMode=UIViewContentModeCenter;


//    _imageViewIII.transform=CGAffineTransformMakeTranslation(100, 200);


}

 //添加按钮控制图片放大回调方法

-(void)fangDa{

    _changeSizeImageView.transform=CGAffineTransformMakeScale(2, 5);

}

   //添加按钮控制图片缩小回调方法

-(void)suoXiao{

    _changeSizeImageView.transform=CGAffineTransformMakeScale(0.2, 0.5);


}

//图片放大回调方法

-(void)zengDa{

    CGRect bonus = _changeFrameImageView.bounds;//图片中心为中点

    bonus.size.width += 30;

    bonus.size.height += 30;

    _changeFrameImageView.bounds = bonus;

}

//图片缩小回调方法

-(void)bianXiao{

    CGRect frame =  _changeFrameImageView.frame;//图片左上角为中心

    frame.size.height -= 30;

    frame.size.width -= 30;

    _changeFrameImageView.frame = frame;

}


//为切换高亮添加的方法

-(void)gaoLiang{

    if (_imageViewIII.highlighted==YES) {

        _imageViewIII.highlighted=NO;

    }else{

        _imageViewIII.highlighted=YES;

    }

}

-(void)shouShi{

    NSLog(@"图片手势方法。。。");

}

//点击图片旋转按钮调用的自定义方法,其中设置图片旋转多少

-(void)xuanZhuan{

    _i+=20;

    _rotationImageView.transform=CGAffineTransformMakeRotation(M_PI*_i/180);

//    这样的旋转是以图片中心为旋转中心的,旋转角度为:M_PI*/180中?位置

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


你可能感兴趣的:(objective-c UIImageView)