昨晚就開始打算? 如果今天有空就研究一下 圖片的放大縮小以及旋轉(zhuǎn)功能是怎么實現(xiàn)的
在沒開始之前 我的思路是:
對于放大縮小
我們先記錄開始我們手指2個點? 然后算距離?
在change的方法里 計算2點的距離? 然后按比例縮放
對于旋轉(zhuǎn)?
先獲取2點? 然后計算2點的角度
如圖
然后我們在change的方法里 也計算角度 然后和原創(chuàng)角度對比 再旋轉(zhuǎn)相應的度數(shù)
然后對于圖片的變好我是想改變frame和layer屬性來實現(xiàn)的
但是 對于我想到的事情蘋果又比我早想到了 = =
實現(xiàn)代碼如下所示:
實現(xiàn)放大縮小的
- (void)handlePinch:(UIPinchGestureRecognizer *)recognizer {
//在已縮放大小基礎下進行累加變化芯急;區(qū)別于:使用 CGAffineTransformMakeScale 方法就是在原大小基礎下進行變化
CGFloat scale = recognizer.scale;
//? ? NSLog(@"1? ==? %f",recognizer.scale);
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, scale, scale);
recognizer.scale = 1.0; //記得設置為1
}
實現(xiàn)旋轉(zhuǎn)的
/**
*? 旋轉(zhuǎn)手勢
*/
- (void)handleRotation:(UIRotationGestureRecognizer *)recognizer {
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
recognizer.rotation = 0.0;//這里記得設置為0
}
創(chuàng)建手勢的代碼 如下所示:
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
[view addGestureRecognizer:pinch];
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotation:)];
[view addGestureRecognizer:rotation];
好了做完自己想要的功能了
總結(jié):1.transform 這屬性可以實現(xiàn)view的放大縮小? 旋轉(zhuǎn) 的功能;
? ? ? ? ? 2.記得設置 recognizer.scale = 1.0;? 和? recognizer.rotation = 0.0;
輕掃手勢 (不是輕掃屏幕邊緣缓熟,是整個屏幕都可以)
//添加輕掃手勢
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//設置輕掃的方向
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; //默認向右
[self.view addGestureRecognizer:swipeGesture];
//添加輕掃手勢
UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//設置輕掃的方向
//? ? swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; //默認向右
[self.view addGestureRecognizer:swipeGestureLeft];
比平常多了一個設置方向的設置
//輕掃手勢觸發(fā)方法
-(void)swipeGesture:(id)sender
{
UISwipeGestureRecognizer *swipe = sender;
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft)
{
NSLog(@"左邊");
}
if (swipe.direction == UISwipeGestureRecognizerDirectionRight)
{
NSLog(@"右邊");
}
}
再把我之前寫的 長按手勢 的代碼也貼上
/**
*? 長按手勢
*/
@property (nonatomic, strong) UILongPressGestureRecognizer *longPress;
_longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(lonePressMoving:)];
[self.JPcollectionView addGestureRecognizer:_longPress];
- (void)lonePressMoving:(UILongPressGestureRecognizer *)longPress {
switch (_longPress.state) {
case UIGestureRecognizerStateBegan: {
{
CLog(@"開始");
}
break;
}
case UIGestureRecognizerStateChanged: {
//拖動
break;
}
case UIGestureRecognizerStateEnded: {
CLog(@"結(jié)束");
break;
}
default: [self.JPcollectionView cancelInteractiveMovement];
break;
}
}
iOS的手勢有七種
UIPanGestureRecognizer(拖動)
UIPinchGestureRecognizer(捏合)
UIRotationGestureRecognizer(旋轉(zhuǎn))
UITapGestureRecognizer(點按)
UILongPressGestureRecognizer(長按)
?UISwipeGestureRecognizer(輕掃)
自定義
自定義的思路: 1.繼承 UIGestureRecognizer 類 (記得導入 #import <UIKit/UIGestureRecognizerSubclass.h > )
????????????????????????? 2.定制下面的方法 ?????
- (void)reset;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
//以上方法在分類 UIGestureRecognizer (UIGestureRecognizerProtected) 中聲明眼溶,更多方法聲明請自行查看
//單擊手勢 (自定義的)
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showPicker)];
tapGesture.numberOfTapsRequired = 1;//手勢敲擊的次數(shù)
[self.view addGestureRecognizer:tapGesture];
然后就寫方法就可以了
UIGestureRecognizer 的繼承關系如下:
手勢狀態(tài)
在前六種手勢識別中,只有一種手勢是離散型手勢聪轿,它就是 UITapGestureRecognizer韭畸。
離散型手勢的特點就是:一旦識別就無法取消白魂,而且只會調(diào)用一次手勢操作事件(初始化手勢時指定的回調(diào)方法)喻犁。
?換句話說其他五種手勢是連續(xù)型手勢槽片,而連續(xù)型手勢的特點就是:會多次調(diào)用手勢操作事件,而且在連續(xù)手勢識別后可以取消手勢肢础。從下圖可以看出兩者調(diào)用操作事件的次數(shù)是不同的:
手勢狀態(tài)枚舉如下:
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
UIGestureRecognizerStatePossible,? // 尚未識別是何種手勢操作(但可能已經(jīng)觸發(fā)了觸摸事件)还栓,默認狀態(tài)
UIGestureRecognizerStateBegan,? ? ? // 手勢已經(jīng)開始,此時已經(jīng)被識別传轰,但是這個過程中可能發(fā)生變化剩盒,手勢操作尚未完成
UIGestureRecognizerStateChanged,? ? // 手勢狀態(tài)發(fā)生轉(zhuǎn)變
UIGestureRecognizerStateEnded,? ? ? // 手勢識別操作完成(此時已經(jīng)松開手指)
UIGestureRecognizerStateCancelled,? // 手勢被取消,恢復到默認狀態(tài)
UIGestureRecognizerStateFailed,? ? // 手勢識別失敗慨蛙,恢復到默認狀態(tài)
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // 手勢識別完成辽聊,同UIGestureRecognizerStateEnded
};
對于離散型手勢 UITapGestureRecgnizer 要么被識別,要么失敗期贫,點按(假設點按次數(shù)設置為1身隐,并且沒有添加長按手勢)下去一次不松開則此時什么也不會發(fā)生,松開手指立即識別并調(diào)用操作事件唯灵,并且狀態(tài)為3(已完成)。
但是連續(xù)型手勢要復雜一些隙轻,就拿旋轉(zhuǎn)手勢來說埠帕,如果兩個手指點下去不做任何操作,此時并不能識別手勢(因為我們還沒旋轉(zhuǎn))但是其實已經(jīng)觸發(fā)了觸摸開始事件玖绿,此時處于狀態(tài)0敛瓷;如果此時旋轉(zhuǎn)會被識別,也就會調(diào)用對應的操作事件斑匪,同時狀態(tài)變成1(手勢開始)呐籽,但是狀態(tài)1只有一瞬間;緊接著狀態(tài)變?yōu)?(因為我們的旋轉(zhuǎn)需要持續(xù)一會)蚀瘸,并且重復調(diào)用操作事件(如果在事件中打印狀態(tài)會重復打印2)狡蝶;松開手指,此時狀態(tài)變?yōu)?贮勃,并調(diào)用1次操作事件贪惹。
下面就說一下裝逼一點的了
手勢穿透
適用場景ViewA 當住了部分ViewB(A在B的上面) 但是A中可以看到B的部分? 然后點了AB重復的部分 B觸發(fā)方法
如何實現(xiàn):
重寫 響應鏈中的 ? -(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;? 方法
這個方法做了什么事情呢?
1.判斷當前控件userInteractionEnabled寂嘉、hidden奏瞬、alpha這三個屬性的值
2.調(diào)用 pointInside: withEvent: 方法
3.從后向前遍歷子控件枫绅,并調(diào)用子控件的 hitTest: withEvent: 和 pointInside: withEvent: 方法
因為??是不開源的 所以猜測的代碼如下
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
??? if (self.userInteractionEnabled == NO || self.hidden == YES || self.alpha <= 0.01) {
??????? return nil;
??? }
??? if ([self pointInside:point withEvent:event] == NO) {
??????? return nil;
??? }
??? int count = (int)self.subviews.count - 1;
??? for (int i = count; i >= 0 ; i--) {
??????? UIView *view = self.subviews[i];
??????? CGPoint p = [view convertPoint:point fromView:self];
??????? if ([view pointInside:p withEvent:event]) {
??????????? return [view hitTest:p withEvent:event];
??????????? break;
???????? }
}
return self;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event;
這個方法的作用就是判斷這個點 它在不在他的范圍內(nèi)? 它能不能響應這個點擊事件
若和實現(xiàn)手勢穿透呢
判斷當前的view是不是我大的那個view? 如果是我就讓這方法繼續(xù)走? 如果不是就返回nil
用黑科技實現(xiàn)手勢穿透
要是情況允許 我們可以設置tag值? 用tag值 和point 來實現(xiàn) 我們想要的效果