你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> ios簡單手勢操作-模態視圖

ios簡單手勢操作-模態視圖

編輯:IOS開發綜合

1.新建一個single view工程

2.ViewController.h

@interface ViewController : UIViewController  {
    UIImageView *_imgView;//展示圖片
}
@end

3.ViewController.m 初始化_imgView

    //初始化image view
    _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    _imgView.backgroundColor = [UIColor yellowColor];
    _imgView.userInteractionEnabled = YES;//一定要記得打開圖片視圖的用戶交互
    [self.view addSubview:_imgView];
    [_imgView release];


4.為_imgView添加手勢操作

    //手勢
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectImage)];
//    tap.numberOfTapsRequired = 2;//雙擊觸發
//    tap.numberOfTouchesRequired = 2;//雙指觸摸觸發
    [_imgView addGestureRecognizer:tap];
    [tap release];

5.實現selectImage方法

- (void)selectImage
{
    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
    ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//設置從哪裡選擇圖片
    ipc.delegate = self;
    [self presentViewController:ipc animated:YES completion:nil];//模態視圖
    [ipc release];
}

6.實現兩個代理方法

//選擇圖片調用
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    _imgView.image = [info objectForKey:UIImagePickerControllerOriginalImage];//
    [self dismissViewControllerAnimated:YES completion:nil];
}

//點擊取消調用
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved