IBActionSheet - ios

源码地址IBActionSheet

最初接触到IBActionSheet是因为用UIActionSheet的时候,项目组有人说会导致内存泄露,在delegate那里,查了一下文档


IBActionSheet - ios_第1张图片
文档介绍

weak 声明,肯定不会引起内存泄露啦。ios8以后已经不赞成使用UIActionSheet了,话说回来,IBActionSheet 是可以自己定制的UIActionSheet,类似ios7风格,而且按钮类型可以自己定制! 系统的添加标题方式如下:

- (NSInteger)addButtonWithTitle:(NSString *)title;

  • (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;

IBActionSheet的就丰富多了

- (NSInteger)addButtonWithTitle:(NSString *)title;

  • (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;
  • (id)initWithTitle:(NSString *)title delegate:(id)delegate cancelButtonTitle:(NSString *)cancelTitle destructiveButtonTitle:(NSString *)destructiveTitle otherButtonTitles:(NSString *)otherTitles, ... NS_REQUIRES_NIL_TERMINATION;
  • (id)initWithTitle:(NSString *)title callback:(IBActionCallback)callback cancelButtonTitle:(NSString *)cancelTitle destructiveButtonTitle:(NSString *)destructiveTitle otherButtonTitles:(NSString *)otherTitles, ... NS_REQUIRES_NIL_TERMINATION;
  • (id)initWithTitle:(NSString *)title delegate:(id)delegate cancelButtonTitle:(NSString *)cancelTitle destructiveButtonTitle:(NSString *)destructiveTitle otherButtonTitlesArray:(NSArray *)otherTitlesArray;
  • (id)initWithTitle:(NSString *)title callback:(IBActionCallback)callback cancelButtonTitle:(NSString *)cancelTitle destructiveButtonTitle:(NSString *)destructiveTitle otherButtonTitlesArray:(NSArray *)otherTitlesArray;
  • (NSInteger)numberOfButtons;
  • (NSString *)buttonTitleAtIndex:(NSInteger)index;
  • (void)rotateToCurrentOrientation;
    // fonts
  • (void)setFont:(UIFont *)font;
  • (void)setTitleFont:(UIFont *)font;
  • (void)setFont:(UIFont *)font forButtonAtIndex:(NSInteger)index;

还有其他添加的方式,主要是支持block,大爱呀!

源码分析:点击黑色半透明区域消失:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelSelection)];
tap.numberOfTapsRequired = 1;
[self.transparentView addGestureRecognizer:tap];

先判断回调函数有木有处理,然后删除,因为删除有动画过程所以单独处理了一下
- (void) cancelSelection {
if(self.callback) self.callback(self, NSIntegerMax);
[self removeFromView];
}

removeFromView
处理消失的过程!
这里因为兼容了ios7以前的版本!所以使用
#define SYSTEM_VERSION_LESS_THAN(version) ([[[UIDevice currentDevice] systemVersion] compare:version options:NSNumericSearch] == NSOrderedAscending)
判断了一下。为了使用ios7才有的动画函数:
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);
增加了阻尼系数,弹性系数,让动画看起来更加真实,逼真!
还有一点比较有趣的函数:

- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
{
    UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds
                                                  byRoundingCorners:corners
                                                        cornerRadii:CGSizeMake(4.0, 4.0)];
    CAShapeLayer *shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];
    view.layer.mask = shape;
}

通常的方法设置圆角,会导致四个边角都会有圆角,如果只要设置一个方向的圆角,可以考虑这种方法。好饿,今天源码读到这里暂时!

你可能感兴趣的:(IBActionSheet - ios)