iOS之悬浮视图:按钮/图片/轮播图/gif图/视频/音频/自定义view
2017-05-10 14:04
633 查看
悬浮按钮:
悬浮图片:
悬浮Gif图:
悬浮轮播图:
悬浮视频:
讲解:(代码片段)
问题一:
对于悬浮视图, 我需要的是在所有的页面都能看到这个悬浮视图,包括二,三级等所有子页面.所以:通过创建一个新的窗口UIWindow. 在一般的的APP中弹框Alert的等级是最高的了,但是悬浮视图的windowLevel比弹框更高.如下代码:
- (UIWindow *)createCustomWindow{ if (!_customWindow) { _customWindow=[[UIWindow alloc]init]; _customWindow.frame=CGRectMake(WINDOWS.width-viewWidth,WINDOWS.height-viewHeight-49, viewWidth, viewHeight); _customWindow.windowLevel=UIWindowLevelAlert+1;//等级加一 _customWindow.backgroundColor=[UIColor clearColor]; } return _customWindow; }
问题二:
对于悬浮视图的类型: 按钮,图片,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;
}
使用:
- (void)viewDidLoad { [super viewDidLoad]; LCSuspendCustomBaseViewController *floatVC=[[LCSuspendCustomBaseViewController alloc]init]; floatVC.suspendType=SCROLLVIEW; [self addChildViewController:floatVC]; [self.view addSubview:floatVC.view]; }
GitHub地址:https://github.com/LuochuanAD/OC-SuspendView
相关文章推荐
- IOS】自定义UIAlertView样式,实现可替换背景和按钮 此博文包含图片此博文包含视频 (2012-10-24 10:23:25)
- iOS自定义alertView,继承自UIView,可以添加子视图,标题图片,文字
- IOS(UI)_UIButton(按钮)和UIImageView(图片视图)
- 富文本编辑器UEditor自定义工具栏(二、插入图片、音频、视频个性化功能按钮和弹层及自定义分页符)
- iOS - 自定义alertView,继承自UIView,可以添加子视图,标题图片+文字
- iOS:自定义图片/视频/音频采集播放,相册,图片编辑
- 【IOS】利用UIActivityViewController实现分享视频,图片,链接
- [iOS]统一设置返回按钮为自定义图片的方法
- iOS -点击按钮查看大图,再次点击还原效果(此处以UITableViewCell为父视图,其中加载多个图片为例)
- iOS开发学习之利用系统远程UITableView 自定义滑动删除按钮、增加自定义滑动按钮方法
- studio自定义SearchView的提交按钮图片
- 自定义ios UIAlertView, 重排按钮位置
- 自定义属性之LinearLayout ImageView TextView模拟图片文字按钮
- IOS自定义UIAlertView样式,实现可替换背景和按钮
- iOS开发备忘录:自定义UINavigationBar背景图片和Back按钮
- iOS uitableview自定义相册(实现拍照并保存在指定目录以相册的形式展示图片)
- ios 地图大头针自定义显示图片 MKAnnotationView
- 自定义动态数量图片显示视图(UIImageView)
- iOS tableHeaderView 图片或视图拉伸
- ios移动开发navigationItem自带的返回按钮设置成自定义图片或者不显示父视图的标题文字