iOS之懸浮檢視:按鈕/圖片/輪播圖/gif圖/視訊/音訊/自定義view
阿新 • • 發佈:2019-01-24
我自定義個
我在touchesEnded:方法中判斷,如果懸浮檢視移動的距離超小,我判斷這是點選手勢, 遵守協議,呼叫點選事件. 所以此時按鈕,圖片,GIF圖就不需要額外的新增點選事件了.此時設定按鈕/圖片/gif圖
LCSuspendCustomView繼承UIView 在這個View中我添加了按鈕(UIButton),圖片(UIImageView),GIF圖(UIWebView)到檢視上.
重寫了touchsbegan:/touchsMoved:/touchsEnded:三個方法.如下:
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [super touchesBegan:touches withEvent:event]; UITouch *touch=[touches anyObject]; _startPoint=[touch locationInView:_rootView]; } - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [super touchesMoved:touches withEvent:event]; UITouch *touch=[touches anyObject]; CGPoint currentPoint=[touch locationInView:_rootView]; self.superview.center=currentPoint; } - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [super touchesEnded:touches withEvent:event]; UITouch *touch=[touches anyObject]; CGPoint currentPoint=[touch locationInView:_rootView];
我在touchesEnded:方法中判斷,如果懸浮檢視移動的距離超小,我判斷這是點選手勢, 遵守協議,呼叫點選事件. 所以此時按鈕,圖片,GIF圖就不需要額外的新增點選事件了.此時設定按鈕/圖片/gif圖
userInteractionEnabled=NO
程式碼如下:if ((pow((_startPoint.x-currentPoint.x), 2)+pow((_startPoint.y-currentPoint.y), 2))<1) { if ([self.suspendDelegate respondsToSelector:@selector(suspendCustomViewClicked:)]) { [self.suspendDelegate suspendCustomViewClicked:self]; } }
問題三:
對於音訊/視訊/自定義View 可以建介面UI新增到
customContentView上.那麼對於介面UI上的一些有用到點選事件,使用者互動的.肯定是userInteractionEnabled=YES.為了不讓自定義的UI阻塞響應鏈. 那麼需要自定義控制元件(按鈕/圖片/webView等等),同時需要重寫touchsbegan:/touchsMoved:/touchsEnded:三個方法. 程式碼如下:
@implementation SuspendButton - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [[self nextResponder]touchesBegan:touches withEvent:event]; } - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [[self nextResponder]touchesMoved:touches withEvent:event]; } - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [[self nextResponder]touchesEnded:touches withEvent:event]; } @end @implementation SuspendScrollView - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [[self nextResponder]touchesBegan:touches withEvent:event]; } - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [[self nextResponder]touchesMoved:touches withEvent:event]; } - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [[self nextResponder]touchesEnded:touches withEvent:event]; } @end @implementation SuspendImageView - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [[self nextResponder]touchesBegan:touches withEvent:event]; } - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [[self nextResponder]touchesMoved:touches withEvent:event]; } - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [[self nextResponder]touchesEnded:touches withEvent:event]; } @end @implementation SuspendView - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [[self nextResponder]touchesBegan:touches withEvent:event]; } - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [[self nextResponder]touchesMoved:touches withEvent:event]; } - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [[self nextResponder]touchesEnded:touches withEvent:event]; } @end
問題四:
想讓懸浮檢視消失.程式碼如下:- (void)cancelWindow{
[_customWindow resignFirstResponder];
_customWindow=nil;
}