iOS开发——封装自己的下拉菜单
2015-05-10 15:11
218 查看
效果图
思路
1、图片的拉伸:
UIImage *image = [UIImage imageNamed:@"popover_background"]; image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(24, 0, 24, 0) resizingMode:UIImageResizingModeStretch];
-resizableImageWithCapInsets:resizingMode方法的第二个参数,调整模式分为两种,平铺与拉伸。这里采用拉伸,可以保证背景的带箭头的框框按照内容大小正确地将其包围。
2、圆形过渡动画
具体实现方式可以参考这篇博客: iOS开发——圆形过渡动画。这里把代码摆上CGRect originalRect = CGRectMake(point.x, point.y, 1, 1); self.startPath = [UIBezierPath bezierPathWithOvalInRect:originalRect]; CGPoint extremePoint = CGPointMake(point.x, point.y + contentView.frame.size.height); CGFloat radius = sqrtf(extremePoint.x * extremePoint.x + extremePoint.y * extremePoint.y); UIBezierPath *endPath = [UIBezierPath bezierPathWithOvalInRect: CGRectInset(originalRect, -radius, -radius)]; self.shapeLayer = [CAShapeLayer layer]; self.shapeLayer.path = endPath.CGPath; self.layer.mask = self.shapeLayer; if (animated) { CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"]; animation.fromValue = (__bridge id)(self.startPath.CGPath); animation.toValue = (__bridge id)(endPath.CGPath); animation.duration = 0.3f; [self.shapeLayer addAnimation:animation forKey:@"begin"]; }
3、其他
①view的层级关系:主要有两个控件,contentView为用户传入的view,一般是一个表给,用户需要自己规定这个contentView的宽高。
另一个是imageView充当背景图片,同时该imageView会作为用户传入的contentView的容器,
整个控件为一个View,背景透明,做mask用,当菜单处于打开状态时用户点击背景会使菜单收回。
当用户传入contentView后,重设其原点,同时更新imageView的大小使其将整个contentView包围。
最后,将这个整体加到window中,保证菜单处于最上层。
②坐标系转换
由用户传入一个控件来决定该菜单显示在哪时,需要用到坐标系转换。关于坐标系转换,之前的博客中也有记录,在此留一个简单的结论:
[viewA convertRect:viewA.bounds toView:viewB]—得到viewA在viewB坐标系下的bounds。
③CAAnimationDelegate中区分多个animation对象
首先在添加animation时为其设置标志key:
[self.shapeLayer addAnimation:animation forKey:@"end"];
在代理方法中根据layer的方法拿:
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag { if (anim == [self.shapeLayer animationForKey:@"end"]) { self.shapeLayer.path = self.startPath.CGPath; [self removeFromSuperview]; } }
代码
源代码相关文章推荐
- iOS开发之自己封装的提示框(警告框)样式BHAlertView
- iOS开发之自己封装的提示框(警告框)样式BHAlertView
- ios开发-新浪微博-09(下拉菜单封装一)
- ios开发-新浪微博10-(下拉菜单的二次封装 完整版)
- iOS开发 - 封装一个自己的按钮
- Android开发-02-自己常用的一个get和post方式提交类的封装
- iOS开发 - 圆形验证码(或密码)输入框的封装
- iOS应用开发,在系统分享列表中加入自己的应用
- ios-新浪微博开发-22-封装标题按钮
- 如何在IOS开发中在自己的framework中添加.bunble文件
- iOS开发 提示框UIAlertController的略微封装
- iOS开发架构封装异步请求案例实现
- iOS开发之保存照片到自己创建的相簿
- iOS 开发使用七牛云存储与自己产品的配合
- iOS开发——post异步网络请求封装
- ios开发——详解sqlite数据库,封装数据库操作
- 移动开发在路上-- IOS移动开发 五 网络请求封装
- iOS开发系列--打造自己的“美图秀秀”
- iOS开发 - 用AVPlayer封装一个播放器
- 在开发iOS程序时,有时候需要将时间格式调整成自己希望的格式,这个时候我们可以用NSDateFormatter类来处理。 例如: //实例化一个NSDateFormatter对象 NSDateFor