objective-c UIImageView

//程序員

//  ViewController.mide

//  UIImageViewspa

//.net

//  Created by 非凡 程序員 on 15/11/12.3d

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

//圖片


#import "ViewController.h"get


@interface ViewController ()it


@end io


@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

相關文章
相關標籤/搜索