1414
1515static CGFloat itemHeight = 50 ;
1616
17- @interface JVTActionSheetView () {}
18- @property (nonatomic ,strong ) NSMutableArray <JVTActionSheetAction *> *actions;
19- @property (nonatomic ,weak ) UIView *headerView;
20- @property (nonatomic ,assign ) CGFloat sheetWidth;
21- @property (nonatomic ,strong ) UIView *sheetView;
22- @property (nonatomic ,weak ) UIView *presentingOnView;
17+ @interface JVTActionSheetView () {
18+ }
19+ @property (nonatomic , strong ) NSMutableArray <JVTActionSheetAction *> *actions;
20+ @property (nonatomic , weak ) UIView *headerView;
21+ @property (nonatomic , assign ) CGFloat sheetWidth;
22+ @property (nonatomic , strong ) UIView *sheetView;
23+ @property (nonatomic , weak ) UIView *presentingOnView;
2324@property (nonatomic , assign ) BOOL isPresented;
2425@end
2526
2627@implementation JVTActionSheetView
2728
28- -(instancetype ) init {
29+ - (instancetype )init {
2930 self = [super init ];
3031 if (self) {
31- self.actions = [NSMutableArray array ]; }
32+ self.actions = [NSMutableArray array ];
33+ }
3234 return self;
3335}
3436
3537- (void )addAction : (JVTActionSheetAction *)action {
3638 [self .actions addObject: action];
3739}
3840
39- -(void )addHeaderView : (UIView *) headerView {
41+ - (void )addHeaderView : (UIView *)headerView {
4042 self.headerView = headerView;
4143}
4244
43- -(void ) dismiss {
45+ - (void )dismiss {
4446 [self endPresentationAnimation ];
4547}
4648
47- -(void ) show {
49+ - (void )show {
4850 [self startPresentationAnimation ];
4951}
5052
51- -(void ) presentOnTopOfView : (UIView *) view {
53+ - (void )presentOnTopOfView : (UIView *)view {
5254 if (self.isPresented ) {
5355 return ;
5456 }
@@ -61,7 +63,6 @@ -(void) presentOnTopOfView:(UIView *) view {
6163 self.sheetView .backgroundColor = [UIColor whiteColor ];
6264
6365 if (self.headerView ) {
64-
6566 CGRect oldSheetViewFrame = self.sheetView .frame ;
6667 oldSheetViewFrame.size .height += self.headerView .frame .size .height ;
6768 self.sheetView .frame = oldSheetViewFrame;
@@ -74,7 +75,6 @@ -(void) presentOnTopOfView:(UIView *) view {
7475 }
7576
7677 for (JVTActionSheetAction *action in self.actions ) {
77-
7878 UIView *itemView = [self itemViewForAction: action];
7979
8080 CGRect oldSheetViewFrame = self.sheetView .frame ;
@@ -88,15 +88,14 @@ -(void) presentOnTopOfView:(UIView *) view {
8888 [self .sheetView addSubview: itemView];
8989 }
9090
91-
9291 [self .sheetView setHidden: YES ];
9392
9493 [view addSubview: self .sheetView];
9594
9695 [self startPresentationAnimation ];
9796}
9897
99- -(void ) startPresentationAnimation {
98+ - (void )startPresentationAnimation {
10099 CGRect startFrame = self.sheetView .frame ;
101100 startFrame.origin .y = self.presentingOnView .bounds .size .height ;
102101 CGRect endFrame = startFrame;
@@ -105,39 +104,48 @@ -(void) startPresentationAnimation {
105104 [self .sheetView setFrame: startFrame];
106105 [self .sheetView setHidden: NO ];
107106
108- [UIView animateWithDuration: 0.4 delay: 0 options: UIViewAnimationOptionCurveEaseInOut animations: ^{
109- [self .sheetView setFrame: endFrame];
110- } completion: ^(BOOL finished) {}];
107+ [UIView animateWithDuration: 0.4
108+ delay: 0
109+ options: UIViewAnimationOptionCurveEaseInOut
110+ animations: ^{
111+ [self .sheetView setFrame: endFrame];
112+ }
113+ completion: ^(BOOL finished){
114+ }];
111115}
112116
113- -(void ) endPresentationAnimation {
117+ - (void )endPresentationAnimation {
114118 CGRect endFrame = self.sheetView .frame ;
115119 endFrame.origin .y = self.presentingOnView .bounds .size .height ;
116-
120+
117121 @weakify (self);
118- [UIView animateWithDuration: 0.3 delay: 0 options: UIViewAnimationOptionCurveEaseInOut animations: ^{
119- @strongify (self);
120- [self .sheetView setFrame: endFrame];
121- } completion: ^(BOOL finished) {
122- @strongify (self);
123- self.isPresented = NO ;
124-
125- if (self.delegate ) {
126- [self .delegate actionSheetDidDismiss ];
127- }
128- }];
122+ [UIView animateWithDuration: 0.3
123+ delay: 0
124+ options: UIViewAnimationOptionCurveEaseInOut
125+ animations: ^{
126+ @strongify (self);
127+ [self .sheetView setFrame: endFrame];
128+ }
129+ completion: ^(BOOL finished) {
130+ @strongify (self);
131+ self.isPresented = NO ;
132+
133+ if (self.delegate ) {
134+ [self .delegate actionSheetDidDismiss ];
135+ }
136+ }];
129137}
130138
131- -(UIView *) itemViewForAction : (JVTActionSheetAction *) action {
139+ - (UIView *)itemViewForAction : (JVTActionSheetAction *)action {
132140 UIView *view = [[UIView alloc ] init ];
133141 CGFloat linePaddingFromLeft = 10 ;
134142 UIView *topLineView = [[UIView alloc ] initWithFrame: CGRectMake (linePaddingFromLeft, 0 , self .sheetWidth - linePaddingFromLeft, 0.5 )];
135- topLineView.backgroundColor = [UIColor colorWithRed: 221.0 / 255.0 green: 221.0 / 255.0 blue: 221.0 / 255.0 alpha: 1.0 ];
143+ topLineView.backgroundColor = [UIColor colorWithRed: 221.0 / 255.0 green: 221.0 / 255.0 blue: 221.0 / 255.0 alpha: 1.0 ];
136144 [view addSubview: topLineView];
137145 UIBlockButton *btn = [[UIBlockButton alloc ] init ];
138146 [btn setTitle: action.title forState: UIControlStateNormal];
139147 [btn.titleLabel setTextAlignment: NSTextAlignmentCenter];
140- [btn setTitleColor: [UIColor colorWithRed: 46.0 / 255.0 green: 145.0 / 255.0 blue: 255.0 / 255.0 alpha: 1.0 ] forState: UIControlStateNormal];
148+ [btn setTitleColor: [UIColor colorWithRed: 46.0 / 255.0 green: 145.0 / 255.0 blue: 255.0 / 255.0 alpha: 1.0 ] forState: UIControlStateNormal];
141149
142150 [view setFrame: CGRectMake (0 , 0 , self .sheetWidth, itemHeight)];
143151 [btn setFrame: view.frame];
@@ -148,16 +156,17 @@ -(UIView *) itemViewForAction:(JVTActionSheetAction *) action {
148156 [btn.titleLabel setFont: [UIFont systemFontOfSize: fontSize weight: fontWeight]];
149157
150158 @weakify (self);
151- [btn handleControlEvent: UIControlEventTouchUpInside withBlock: ^{
152- @strongify (self);
153- [self endPresentationAnimation ];
154- action.handler (action);
155- }];
159+ [btn handleControlEvent: UIControlEventTouchUpInside
160+ withBlock: ^{
161+ @strongify (self);
162+ [self endPresentationAnimation ];
163+ action.handler (action);
164+ }];
156165
157166 return view;
158167}
159168
160- -(void ) didTapOnDimmedBackground {
169+ - (void )didTapOnDimmedBackground {
161170 [self dismiss ];
162171}
163172
0 commit comments