#iOS開發筆記#UIImageView實現拖動,放大/縮小,旋轉
UIImageView是iOS上最基本的圖片顯示控制元件,但預設情況下是不支援拖動,放大縮小等功能的。後來在stackoverflow上查詢到一種很簡單的方法,加上本人自己的理解,步驟整理如下:
1.開啟userInteractionEnabled
-
New image view objects are configured to disregard user events by default. If you want to handle events in a custom subclass of
UIImageView
, you must explicitly change the value of the property toYES
所以首先第一步要set這個屬性
[imageView setUserInteractionEnabled:YES];
2.在當前controller中新增UIGestureRecognizerDelegate協議
@interface MyViewController : UIViewController <UIGestureRecognizerDelegate>
3.重寫gestureRecognizer方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; }
4.定義手勢引發的操作方法。UIImageView物件主要應用三種手勢操作:UIPinchGestureRecognizer,UIPanGestureRecognizer和UIRotationGestureRecognizer,分別對應(二指)放大縮小,拖動和(二指)旋轉的手勢操作。三個對應方法如下:
- (void)pinchGestureDetected:(UIPinchGestureRecognizer *)recognizer { UIGestureRecognizerState state = [recognizer state]; if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged) { CGFloat scale = [recognizer scale]; [recognizer.view setTransform:CGAffineTransformScale(recognizer.view.transform, scale, scale)]; [recognizer setScale:1.0]; } } - (void)panGestureDetected:(UIPanGestureRecognizer *)recognizer { UIGestureRecognizerState state = [recognizer state]; if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged) { CGPoint translation = [recognizer translationInView:recognizer.view]; [recognizer.view setTransform:CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y)]; [recognizer setTranslation:CGPointZero inView:recognizer.view]; } } - (void)rotationGestureDetected:(UIRotationGestureRecognizer *)recognizer { UIGestureRecognizerState state = [recognizer state]; if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged) { CGFloat rotation = [recognizer rotation]; [recognizer.view setTransform:CGAffineTransformRotate(recognizer.view.transform, rotation)]; [recognizer setRotation:0]; } }
5.新建三種recognizer物件,分別呼叫上一步的三種方法,並繫結到UIImageView物件中
UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGestureDetected:)];
[pinchGestureRecognizer setDelegate:self];
[imageView addGestureRecognizer:pinchGestureRecognizer];
UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureDetected:)];
[rotationGestureRecognizer setDelegate:self];
[imageView addGestureRecognizer:rotationGestureRecognizer];
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureDetected:)];
[panGestureRecognizer setDelegate:self];
[imageView addGestureRecognizer:panGestureRecognizer];
Done. 經過上述五步,imageView已經能實現放大縮小,拖動和旋轉的功能,全部參考程式碼如下:
MyViewController.h
@interface MyViewController : UIViewController <UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
...
@end
MyViewController.m
@implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self enableGestureOnView:self.imageView];
}
#pragma mark - View Gesture Process
- (void)enableGestureOnView:(UIView *)view{
[view setUserInteractionEnabled:YES];
// create and configure the pinch gesture
UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGestureDetected:)];
[pinchGestureRecognizer setDelegate:self];
[view addGestureRecognizer:pinchGestureRecognizer];
// create and configure the rotation gesture
UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureDetected:)];
[rotationGestureRecognizer setDelegate:self];
[view addGestureRecognizer:rotationGestureRecognizer];
// creat and configure the pan gesture
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureDetected:)];
[panGestureRecognizer setDelegate:self];
[view addGestureRecognizer:panGestureRecognizer];
}
- (void)pinchGestureDetected:(UIPinchGestureRecognizer *)recognizer
{
UIGestureRecognizerState state = [recognizer state];
if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
{
CGFloat scale = [recognizer scale];
[recognizer.view setTransform:CGAffineTransformScale(recognizer.view.transform, scale, scale)];
[recognizer setScale:1.0];
}
}
- (void)rotationGestureDetected:(UIRotationGestureRecognizer *)recognizer
{
UIGestureRecognizerState state = [recognizer state];
if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
{
CGFloat rotation = [recognizer rotation];
[recognizer.view setTransform:CGAffineTransformRotate(recognizer.view.transform, rotation)];
[recognizer setRotation:0];
}
}
- (void)panGestureDetected:(UIPanGestureRecognizer *)recognizer
{
UIGestureRecognizerState state = [recognizer state];
if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
{
CGPoint translation = [recognizer translationInView:recognizer.view];
[recognizer.view setTransform:CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y)];
[recognizer setTranslation:CGPointZero inView:recognizer.view];
}
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
參考資料:
- http://stackoverflow.com/questions/3907397/uigesturerecognizer-on-uiimageview/18622373#18622373
- https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImageView_Class/index.html
相關推薦
#iOS開發筆記#UIImageView實現拖動,放大/縮小,旋轉
UIImageView是iOS上最基本的圖片顯示控制元件,但預設情況下是不支援拖動,放大縮小等功能的。後來在stackoverflow上查詢到一種很簡單的方法,加上本人自己的理解,步驟整理如下: 1.開啟userInteractionEnabled New im
百度地圖實現拖動獲取定位度,實現地位
如果需要實現定位,則需要獲取的是ak,這是實現定位的前提, 像這樣的就是獲取到了ak,也就是一個專案中使用的是一個ak。 申請ak: ==>定位點動畫效果 marker.setAnima
iOS開發筆記--UIImageView的屬性之animationImages詳解
animationImages是陣列型別,該陣列必須包含的UIImage物件。您可以使用相同的影象物件多次在陣中。 例如:將一系列幀新增到一個數組裡面,然後設定animation一系列屬性,如動畫時間,動畫重複次數,還是看程式碼吧,直觀 NSArray *magesArr
iOS開發筆記:實現點選圖片放大全屏
#import "ViewController.h" #import <QuartzCore/QuartzCore.h> //設定放大後圖片的寬高,為了省時間,偷了下懶,建議最好結合實際做下運算 #define BIG_IMG_WIDTH 325 #define BIG_IMG_HEI
[iOS開發專案-3] 按鈕控制元件的移動,放大縮小,左右旋轉操作
本專案是取自傳智播客的教學專案,加入筆者的修改和潤飾。 1. 專案名稱:按鈕操作 2. 專案截圖展示 3. 專案功能 點選按鈕,切換按鈕狀態 上下左右移動按鈕 放大,縮小按鈕 左右旋轉按鈕 4. 專案程式碼 #impor
Qt自定義標題欄詳細介紹(可拖動、放大縮小、關閉、標題欄具有漸變色)
前言 使用Qt自帶的標題欄可能沒有辦法適合我們的需求,例如標題欄的顏色,標題欄的寬度、高度,標題欄的放大、縮小、還原、關閉按鈕等都沒有辦法發生改變。因為預設的標題欄是和作業系統相關的、它會根據作業系統的變化而發生變化,在Window上不同風格的主題,在Qt程式
uwp開發:Slider控制元件和MediaElement繫結,實現拖動播放.
1、實現原理: Slider拖動時,Value值改變,MediaElement播放時,Position值改變。所以,只需將Slider的Value屬性和MediaElement的Position屬性進行繫結即可。 2、實現方法: Slider的Value屬性是dou
vue+mousemove實現拖動,鼠標移動過快拖動就失效
拖動 win text cli prev point element lis 問題 今天用vue+原生js的mousemove事件,寫了個拖動,發現只能慢慢拖動才行,鼠標只要移動快了,就失效,不能拖動了; 搞了半天在,總算解決了,但是問題的深層原理還沒搞清楚,知道的大俠可以
windows中拖動檔案到瀏覽器,實現自動上傳 -- Java
//拖動License檔案到瀏覽器中,自動讀取檔案並上傳var dropbox = $("body")[0];dropbox.addEventListener("dragenter", function(e){ e.stopPropagation(); e.preventDefault();},
iOS開發筆記--Objective-C實現多繼承
我們都知道objective C不能像C++一樣支援多繼承,但是在OC的使用經常會碰到需要使用多繼承的情況。例如,ClassA中有methodA,ClassB中methodB,而現在需要使用這兩個類中的方法。如何按照C++的程式設計思路,毫無疑問採用多繼承就搞定
Android 實現高仿iOS桌面效果之可拖動的GridView(上)
最近專案中遇到一個LIstview的拖動效果,github上一搜發現有叫DragListview的開源專案,然後自己再小手一搜拖動排序的GridView,卻沒發現什麼很全很好的開源專案,後
ios開發:怎麼實現點選一個按鈕,跳轉到一個新的介面,並回退到上一介面
iOS三種檢視切換的原理各不相同:UITabBarController:以平行的方式管理檢視,各個檢視之間往往關係並不大,每個加入到UITabBarController的檢視都會進行初始化即使當前不顯示在介面上,相對比較佔用記憶體。UINavigationControlle
JFrame實現無邊框,實現拖動,實現關閉按鈕,開啟實現儲存檔案到本地,實現頭像放大功能,實現圖片瀏覽器
package SwingTest3; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.even
#iOS開發筆記#如何限制UIImageView放大縮小的比例
當一個UIImageView實現了手勢操作並可以用兩指實現放大縮小之後,如何能限制放大縮小的比例? 來源: http://stackoverflow.com/a/5446348/2177332 程式碼實現: @property CGFloat lastScale;
如何重寫PictureBox的OnPaint方法繪製矩形,並實現拖動,滑鼠懸停時提示拖拽方向
使用Graphic繪製可拖動的矩形框(滑鼠懸停可以顯示拖動提示) 首先做繪圖前的準備 新建winform程式,在解決方案上右擊新增使用者控制元件,然後同樣的方法新增元件並繼承於使用者控制元件; 一切準備好後,在Form窗體中將新增的使用者控制元件拖拽到窗體
原生js實現拖動滑塊驗證
cnblogs tcc mvt wms 網站 hnu 按鈕 itl rip 拖動滑塊驗證是現在的網站隨處可見的,各式各樣的拖動法都有。 下面實現的是某寶的拖動滑塊驗證: <!DOCTYPE html> <html lang="en"> <he
iOS開發各種底層實現--面試必備!
task 源碼 控件 改變 消息發送 釋放內存 retain select 匹配 iOS開發常用技術底層實現(精簡概述) 本章將對ios開發技術底層實現的總結,其實關於ios開發中各種底層的實現,網上相關文章多到數不過來,而不且非常不錯,我也沒有自信我能比他們做的更好,因
RecyclerView借助ItemTouchHelper實現拖動和滑動刪除功能
enable all istview aslist mar -- main erl pub RecyclerView是官方推薦代替ListView的空間,怎樣實現RecyclerView列表元素的拖動呢? 官方提供了ItemTouchHelper類使用過程例如以下
iOS開發筆記17:自定義相機拍照
hub 銷毀 用戶 做了 api 交互設計 cap iphone 結果 之前用AVFoundation自定義相機做了拍照與視頻相關的東西,為什麽要自定義呢?主要是提供更個性化的交互設計,符合app主題,對於視頻來說,也便於提供更多豐富有趣的功能。前段時間整理了下拍照部分的功
vue實現拖動div元素
() left pan down htm 元素 null col color html: <div id="app1"> <div v-drag class="drag"></div> <div v-drag