IBActionSheet - ios

源碼地址IBActionSheet

最初接觸到IBActionSheet是因為用UIActionSheet的時候,項目組有人說會導(dǎo)致內(nèi)存泄露,在delegate那里,查了一下文檔


文檔介紹

weak 聲明,肯定不會引起內(nèi)存泄露啦。ios8以后已經(jīng)不贊成使用UIActionSheet了,話說回來,IBActionSheet 是可以自己定制的UIActionSheet,類似ios7風(fēng)格,而且按鈕類型可以自己定制! 系統(tǒng)的添加標題方式如下:
<pre><code>- (NSInteger)addButtonWithTitle:(NSString *)title;

  • (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;
    </code></pre>

IBActionSheet的就豐富多了
<pre><code>- (NSInteger)addButtonWithTitle:(NSString *)title;

  • (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;
  • (id)initWithTitle:(NSString *)title delegate:(id<IBActionSheetDelegate>)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<IBActionSheetDelegate>)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;
    </code></pre>
    還有其他添加的方式,主要是支持block,大愛呀!

源碼分析:點擊黑色半透明區(qū)域消失:
<pre><code>UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelSelection)];
tap.numberOfTapsRequired = 1;
[self.transparentView addGestureRecognizer:tap];</code></pre>
先判斷回調(diào)函數(shù)有木有處理,然后刪除,因為刪除有動畫過程所以單獨處理了一下
<pre><code>- (void) cancelSelection {
if(self.callback) self.callback(self, NSIntegerMax);
[self removeFromView];
}
</code></pre>

<pre><code>removeFromView</code></pre>處理消失的過程!
這里因為兼容了ios7以前的版本!所以使用<pre><code>#define SYSTEM_VERSION_LESS_THAN(version) ([[[UIDevice currentDevice] systemVersion] compare:version options:NSNumericSearch] == NSOrderedAscending)
</code></pre>判斷了一下。為了使用ios7才有的動畫函數(shù):<pre><code>+ (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);
</code></pre>增加了阻尼系數(shù),彈性系數(shù),讓動畫看起來更加真實,逼真!
還有一點比較有趣的函數(shù):

- (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;
}

通常的方法設(shè)置圓角,會導(dǎo)致四個邊角都會有圓角,如果只要設(shè)置一個方向的圓角,可以考慮這種方法。好餓,今天源碼讀到這里暫時!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容