12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040 |
- //
- // JGActionSheet.m
- // JGActionSheet
- //
- // Created by Jonas Gessner on 25.07.14.
- // Copyright (c) 2014 Jonas Gessner. All rights reserved.
- //
- #import "JGActionSheet.h"
- #import <QuartzCore/QuartzCore.h>
- #if !__has_feature(objc_arc)
- #error "JGActionSheet requires ARC!"
- #endif
- #pragma mark - Defines
- #ifndef kCFCoreFoundationVersionNumber_iOS_7_0
- #define kCFCoreFoundationVersionNumber_iOS_7_0 838.00
- #endif
- #ifndef __IPHONE_8_0
- #define __IPHONE_8_0 80000
- #endif
- #ifndef kBaseSDKiOS8
- #define kBaseSDKiOS8 (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0)
- #endif
- #ifndef iOS8
- #define iOS8 ([UIVisualEffectView class] != Nil)
- #endif
- #ifndef iOS7
- #define iOS7 (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_7_0)
- #endif
- #ifndef rgba
- #define rgba(r, g, b, a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
- #endif
- #ifndef rgb
- #define rgb(r, g, b) rgba(r, g, b, 1.0f)
- #endif
- #ifndef iPad
- #define iPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
- #endif
- #define kHostsCornerRadius 3.0f
- #define kSpacing 12.0f
- #define kArrowBaseWidth 20.0f
- #define kArrowHeight 10.0f
- #define kShadowRadius 4.0f
- #define kShadowOpacity 0.2f
- #define kFixedWidth 320.0f
- #define kFixedWidthContinuous 300.0f
- #define kAnimationDurationForSectionCount(count) MAX(0.22f, MIN(count*0.12f, 0.45f))
- #pragma mark - Helpers
- @interface JGButton : UIButton
- @property (nonatomic, assign) NSUInteger row;
- @end
- @implementation JGButton
- @end
- NS_INLINE UIBezierPath *trianglePath(CGRect rect, JGActionSheetArrowDirection arrowDirection, BOOL closePath) {
- UIBezierPath *path = [UIBezierPath bezierPath];
-
- if (arrowDirection == JGActionSheetArrowDirectionBottom) {
- [path moveToPoint:CGPointZero];
- [path addLineToPoint:(CGPoint){CGRectGetWidth(rect)/2.0f, CGRectGetHeight(rect)}];
- [path addLineToPoint:(CGPoint){CGRectGetWidth(rect), 0.0f}];
- }
- else if (arrowDirection == JGActionSheetArrowDirectionLeft) {
- [path moveToPoint:(CGPoint){CGRectGetWidth(rect), 0.0f}];
- [path addLineToPoint:(CGPoint){0.0f, CGRectGetHeight(rect)/2.0f}];
- [path addLineToPoint:(CGPoint){CGRectGetWidth(rect), CGRectGetHeight(rect)}];
- }
- else if (arrowDirection == JGActionSheetArrowDirectionRight) {
- [path moveToPoint:CGPointZero];
- [path addLineToPoint:(CGPoint){CGRectGetWidth(rect), CGRectGetHeight(rect)/2.0f}];
- [path addLineToPoint:(CGPoint){0.0f, CGRectGetHeight(rect)}];
- }
- else if (arrowDirection == JGActionSheetArrowDirectionTop) {
- [path moveToPoint:(CGPoint){0.0f, CGRectGetHeight(rect)}];
- [path addLineToPoint:(CGPoint){CGRectGetWidth(rect)/2.0f, 0.0f}];
- [path addLineToPoint:(CGPoint){CGRectGetWidth(rect), CGRectGetHeight(rect)}];
- }
-
- if (closePath) {
- [path closePath];
- }
-
- return path;
- }
- static BOOL disableCustomEasing = NO;
- @interface JGActionSheetLayer : CAShapeLayer
- @end
- @implementation JGActionSheetLayer
- - (void)addAnimation:(CAAnimation *)anim forKey:(NSString *)key {
- if (!disableCustomEasing && [anim isKindOfClass:[CABasicAnimation class]]) {
- CAMediaTimingFunction *func = [CAMediaTimingFunction functionWithControlPoints:0.215f: 0.61f: 0.355f: 1.0f];
-
- anim.timingFunction = func;
- }
-
- [super addAnimation:anim forKey:key];
- }
- @end
- @interface JGActionSheetTriangle : UIView
- - (void)setFrame:(CGRect)frame arrowDirection:(JGActionSheetArrowDirection)direction;
- @end
- @implementation JGActionSheetTriangle
- - (void)setFrame:(CGRect)frame arrowDirection:(JGActionSheetArrowDirection)direction {
- self.frame = frame;
-
- [((CAShapeLayer *)self.layer) setPath:trianglePath(frame, direction, YES).CGPath];
- self.layer.shadowPath = trianglePath(frame, direction, NO).CGPath;
-
- BOOL leftOrRight = (direction == JGActionSheetArrowDirectionLeft || direction == JGActionSheetArrowDirectionRight);
-
- CGRect pathRect = (CGRect){CGPointZero, {CGRectGetWidth(frame)+(leftOrRight ? kShadowRadius+1.0f : 2.0f*(kShadowRadius+1.0f)), CGRectGetHeight(frame)+(leftOrRight ? 2.0f*(kShadowRadius+1.0f) : kShadowRadius+1.0f)}};
-
- if (direction == JGActionSheetArrowDirectionTop) {
- pathRect.origin.y -= kShadowRadius+1.0f;
- }
- else if (direction == JGActionSheetArrowDirectionLeft) {
- pathRect.origin.x -= kShadowRadius+1.0f;
- }
-
- UIBezierPath *path = [UIBezierPath bezierPathWithRect:pathRect];
-
- CAShapeLayer *mask = [CAShapeLayer layer];
- mask.path = path.CGPath;
- mask.fillColor = [UIColor blackColor].CGColor;
-
- self.layer.mask = mask;
- self.layer.shadowColor = [UIColor blackColor].CGColor;
- self.layer.shadowOffset = CGSizeZero;
- self.layer.shadowRadius = kShadowRadius;
- self.layer.shadowOpacity = kShadowOpacity;
-
- self.layer.contentsScale = [UIScreen mainScreen].scale;
- ((CAShapeLayer *)self.layer).fillColor = [UIColor whiteColor].CGColor;
- }
- + (Class)layerClass {
- return [JGActionSheetLayer class];
- }
- @end
- @interface JGActionSheetView : UIView
- @end
- @implementation JGActionSheetView
- + (Class)layerClass {
- return [JGActionSheetLayer class];
- }
- @end
- #pragma mark - JGActionSheetSection
- @interface JGActionSheetSection ()
- @property (nonatomic, assign) NSUInteger index;
- @property (nonatomic, copy) void (^buttonPressedBlock)(NSIndexPath *indexPath);
- - (void)setUpForContinuous:(BOOL)continuous;
- @end
- @implementation JGActionSheetSection
- #pragma mark Initializers
- + (instancetype)cancelSection {
- return [self sectionWithTitle:nil message:nil buttonTitles:@[NSLocalizedString(@"Cancel",)] buttonStyle:JGActionSheetButtonStyleCancel];
- }
- + (instancetype)sectionWithTitle:(NSString *)title message:(NSString *)message buttonTitles:(NSArray *)buttonTitles buttonStyle:(JGActionSheetButtonStyle)buttonStyle {
- return [[self alloc] initWithTitle:title message:message buttonTitles:buttonTitles buttonStyle:buttonStyle];
- }
- - (instancetype)initWithTitle:(NSString *)title message:(NSString *)message buttonTitles:(NSArray *)buttonTitles buttonStyle:(JGActionSheetButtonStyle)buttonStyle {
- self = [super init];
-
- if (self) {
- if (title) {
- UILabel *titleLabel = [[UILabel alloc] init];
- titleLabel.backgroundColor = [UIColor clearColor];
- titleLabel.textAlignment = NSTextAlignmentCenter;
- titleLabel.font = [UIFont boldSystemFontOfSize:14.0f];
- titleLabel.textColor = [UIColor blackColor];
- titleLabel.numberOfLines = 1;
-
- titleLabel.text = title;
-
- _titleLabel = titleLabel;
-
- [self addSubview:_titleLabel];
- }
-
- if (message) {
- UILabel *messageLabel = [[UILabel alloc] init];
- messageLabel.backgroundColor = [UIColor clearColor];
- messageLabel.textAlignment = NSTextAlignmentCenter;
- messageLabel.font = [UIFont systemFontOfSize:14.0f];
- messageLabel.textColor = [UIColor grayColor];
- messageLabel.numberOfLines = 0;
-
- messageLabel.text = message;
-
- _messageLabel = messageLabel;
-
- [self addSubview:_messageLabel];
- }
-
- if (buttonTitles.count) {
- NSMutableArray *buttons = [NSMutableArray arrayWithCapacity:buttonTitles.count];
-
- NSInteger index = 0;
-
- for (NSString *str in buttonTitles) {
- JGButton *b = [self makeButtonWithTitle:str style:buttonStyle];
- b.row = (NSUInteger)index;
-
- [self addSubview:b];
-
- [buttons addObject:b];
-
- index++;
- }
-
- _buttons = buttons.copy;
- }
- }
-
- return self;
- }
- + (instancetype)sectionWithTitle:(NSString *)title message:(NSString *)message contentView:(UIView *)contentView {
- return [[self alloc] initWithTitle:title message:message contentView:contentView];
- }
- - (instancetype)initWithTitle:(NSString *)title message:(NSString *)message contentView:(UIView *)contentView {
- self = [super init];
-
- if (self) {
- if (title) {
- UILabel *titleLabel = [[UILabel alloc] init];
- titleLabel.backgroundColor = [UIColor clearColor];
- titleLabel.textAlignment = NSTextAlignmentCenter;
- titleLabel.font = [UIFont boldSystemFontOfSize:14.0f];
- titleLabel.textColor = [UIColor blackColor];
- titleLabel.numberOfLines = 1;
-
- titleLabel.text = title;
-
- _titleLabel = titleLabel;
-
- [self addSubview:_titleLabel];
- }
-
- if (message) {
- UILabel *messageLabel = [[UILabel alloc] init];
- messageLabel.backgroundColor = [UIColor clearColor];
- messageLabel.textAlignment = NSTextAlignmentCenter;
- messageLabel.font = [UIFont systemFontOfSize:12.0f];
- messageLabel.textColor = [UIColor blackColor];
- messageLabel.numberOfLines = 0;
-
- messageLabel.text = message;
-
- _messageLabel = messageLabel;
-
- [self addSubview:_messageLabel];
- }
-
- _contentView = contentView;
-
- [self addSubview:self.contentView];
- }
-
- return self;
- }
- #pragma mark UI
- - (void)setUpForContinuous:(BOOL)continuous {
- if (continuous) {
- self.backgroundColor = [UIColor clearColor];
- self.layer.cornerRadius = 0.0f;
- self.layer.shadowOpacity = 0.0f;
- }
- else {
- self.backgroundColor = [UIColor whiteColor];
- // self.layer.cornerRadius = kHostsCornerRadius;
-
- self.layer.shadowColor = [UIColor blackColor].CGColor;
- self.layer.shadowOffset = CGSizeZero;
- self.layer.shadowRadius = kShadowRadius;
- self.layer.shadowOpacity = kShadowOpacity;
- }
- }
- - (void)setButtonStyle:(JGActionSheetButtonStyle)buttonStyle forButtonAtIndex:(NSUInteger)index {
- if (index < self.buttons.count) {
- UIButton *button = self.buttons[index];
-
- [self setButtonStyle:buttonStyle forButton:button];
- }
- else {
- NSLog(@"ERROR: Index out of bounds");
- return;
- }
- }
- - (UIImage *)pixelImageWithColor:(UIColor *)color {
- UIGraphicsBeginImageContextWithOptions((CGSize){1.0f, 1.0f}, YES, 0.0f);
-
- [color setFill];
-
- [[UIBezierPath bezierPathWithRect:(CGRect){CGPointZero, {1.0f, 1.0f}}] fill];
-
- UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
-
- UIGraphicsEndImageContext();
-
- return [img resizableImageWithCapInsets:UIEdgeInsetsZero];
- }
- - (void)setButtonStyle:(JGActionSheetButtonStyle)buttonStyle forButton:(UIButton *)button {
- UIColor *backgroundColor, *borderColor, *titleColor = nil;
- UIFont *font = nil;
-
- if (buttonStyle == JGActionSheetButtonStyleDefault) {
- font = [UIFont systemFontOfSize:15.0f];
- titleColor = [UIColor blackColor];
-
- backgroundColor = [UIColor colorWithWhite:0.95f alpha:1.0f];
- borderColor = [UIColor colorWithWhite:0.9f alpha:1.0f];
- }
- else if (buttonStyle == JGActionSheetButtonStyleCancel) {
- font = [UIFont boldSystemFontOfSize:15.0f];
- titleColor = [UIColor blackColor];
-
- backgroundColor = [UIColor colorWithWhite:0.95f alpha:1.0f];
- borderColor = [UIColor colorWithWhite:0.9f alpha:1.0f];
- }
- else if (buttonStyle == JGActionSheetButtonStyleRed) {
- font = [UIFont systemFontOfSize:15.0f];
- titleColor = [UIColor whiteColor];
-
- backgroundColor = rgb(231.0f, 76.0f, 60.0f);
- borderColor = rgb(192.0f, 57.0f, 43.0f);
- }
- else if (buttonStyle == JGActionSheetButtonStyleGreen) {
- font = [UIFont systemFontOfSize:15.0f];
- titleColor = [UIColor whiteColor];
-
- backgroundColor = rgb(46.0f, 204.0f, 113.0f);
- borderColor = rgb(39.0f, 174.0f, 96.0f);
- }
- else if (buttonStyle == JGActionSheetButtonStyleBlue) {
- font = [UIFont systemFontOfSize:15.0f];
- titleColor = [UIColor whiteColor];
-
- backgroundColor = rgb(52.0f, 152.0f, 219.0f);
- borderColor = rgb(41.0f, 128.0f, 185.0f);
- }
-
- [button setTitleColor:titleColor forState:UIControlStateNormal];
-
- button.titleLabel.font = font;
-
- [button setBackgroundImage:[self pixelImageWithColor:backgroundColor] forState:UIControlStateNormal];
- [button setBackgroundImage:[self pixelImageWithColor:borderColor] forState:UIControlStateHighlighted];
-
- button.layer.borderColor = borderColor.CGColor;
- }
- - (JGButton *)makeButtonWithTitle:(NSString *)title style:(JGActionSheetButtonStyle)style {
- JGButton *b = [[JGButton alloc] init];
-
- b.layer.cornerRadius = 2.0f;
- b.layer.masksToBounds = YES;
- b.layer.borderWidth = 1.0f;
-
- [b setTitle:title forState:UIControlStateNormal];
-
- [b addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
-
- [self setButtonStyle:style forButton:b];
-
- return b;
- }
- - (void)buttonPressed:(JGButton *)button {
- if (self.buttonPressedBlock) {
- self.buttonPressedBlock([NSIndexPath indexPathForRow:(NSInteger)button.row inSection:(NSInteger)self.index]);
- }
- }
- - (CGRect)layoutForWidth:(CGFloat)width {
- CGFloat buttonHeight = 40.0f;
- CGFloat spacing = kSpacing;
-
- CGFloat height = 0.0f;
-
- if (self.titleLabel) {
- height += spacing;
-
- [self.titleLabel sizeToFit];
- height += CGRectGetHeight(self.titleLabel.frame);
-
- self.titleLabel.frame = (CGRect){{spacing, spacing}, {width-spacing*2.0f, CGRectGetHeight(self.titleLabel.frame)}};
- }
-
- if (self.messageLabel) {
- height += spacing;
-
- CGSize maxLabelSize = {width-spacing*2.0f, width};
-
- CGFloat messageLabelHeight = 0.0f;
-
- if (iOS7) {
- NSDictionary *attributes = @{NSFontAttributeName : self.messageLabel.font};
-
- messageLabelHeight = CGRectGetHeight([self.messageLabel.text boundingRectWithSize:maxLabelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil]);
- }
- else {
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wdeprecated-declarations"
- messageLabelHeight = [self.messageLabel.text sizeWithFont:self.messageLabel.font constrainedToSize:maxLabelSize lineBreakMode:self.messageLabel.lineBreakMode].height;
- #pragma clang diagnostic pop
- }
-
- self.messageLabel.frame = (CGRect){{spacing, height}, {width-spacing*2.0f, messageLabelHeight}};
-
- height += messageLabelHeight;
- }
-
- for (UIButton *button in self.buttons) {
- height += spacing;
-
- button.frame = (CGRect){{spacing, height}, {width-spacing*2.0f, buttonHeight}};
-
- height += buttonHeight;
- }
-
- if (self.contentView) {
- height += spacing;
-
- self.contentView.frame = (CGRect){{spacing, height}, {width-spacing*2.0f, self.contentView.frame.size.height}};
-
- height += CGRectGetHeight(self.contentView.frame);
- }
-
- height += spacing;
-
- self.frame = (CGRect){CGPointZero, {width, height}};
-
- return self.frame;
- }
- @end
- #pragma mark - JGActionSheet
- @interface JGActionSheet () <UIGestureRecognizerDelegate> {
- UIScrollView *_scrollView;
- JGActionSheetTriangle *_arrowView;
- JGActionSheetView *_scrollViewHost;
-
- CGRect _finalContentFrame;
-
- UIColor *_realBGColor;
-
- BOOL _anchoredAtPoint;
- CGPoint _anchorPoint;
- JGActionSheetArrowDirection _anchoredArrowDirection;
- }
- @end
- @implementation JGActionSheet
- @dynamic visible;
- #pragma mark Initializers
- + (instancetype)actionSheetWithSections:(NSArray *)sections {
- return [[self alloc] initWithSections:sections];
- }
- - (instancetype)initWithSections:(NSArray *)sections {
- NSAssert(sections.count > 0, @"Must at least provide 1 section");
-
- self = [super init];
-
- if (self) {
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
- tap.delegate = self;
-
- [self addGestureRecognizer:tap];
-
- _scrollViewHost = [[JGActionSheetView alloc] init];
- _scrollViewHost.backgroundColor = [UIColor clearColor];
-
- _scrollView = [[UIScrollView alloc] init];
- _scrollView.backgroundColor = [UIColor clearColor];
- _scrollView.showsHorizontalScrollIndicator = NO;
- _scrollView.showsVerticalScrollIndicator = NO;
-
- [_scrollViewHost addSubview:_scrollView];
- [self addSubview:_scrollViewHost];
-
- self.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.6f];
-
- _sections = sections;
-
- NSInteger index = 0;
-
- __weak __typeof(self) weakSelf = self;
-
- void (^pressedBlock)(NSIndexPath *) = ^(NSIndexPath *indexPath) {
- [weakSelf buttonPressed:indexPath];
- };
-
- for (JGActionSheetSection *section in self.sections) {
- section.index = index;
-
- [_scrollView addSubview:section];
-
- [section setButtonPressedBlock:pressedBlock];
-
- index++;
- }
- }
-
- return self;
- }
- #pragma mark Overrides
- + (Class)layerClass {
- return [JGActionSheetLayer class];
- }
- - (void)dealloc {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- - (void)setBackgroundColor:(UIColor *)backgroundColor {
- [super setBackgroundColor:backgroundColor];
- _realBGColor = backgroundColor;
- }
- #pragma mark Callbacks
- - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
- if ([self hitTest:[gestureRecognizer locationInView:self] withEvent:nil] == self && self.outsidePressBlock) {
- return YES;
- }
-
- return NO;
- }
- - (void)tapped:(UITapGestureRecognizer *)gesture {
- if ([self hitTest:[gesture locationInView:self] withEvent:nil] == self && self.outsidePressBlock) {
- self.outsidePressBlock(self);
- }
- }
- - (void)orientationChanged {
- if (_targetView && !CGRectEqualToRect(self.bounds, _targetView.bounds)) {
- disableCustomEasing = YES;
- [UIView animateWithDuration:(iPad ? 0.4 : 0.3) delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
- if (_anchoredAtPoint) {
- [self moveToPoint:_anchorPoint arrowDirection:_anchoredArrowDirection animated:NO];
- }
- else {
- [self layoutSheetInitial:NO];
- }
- } completion:^(BOOL finished) {
- disableCustomEasing = NO;
- }];
- }
- }
- - (void)buttonPressed:(NSIndexPath *)indexPath {
- if (self.buttonPressedBlock) {
- self.buttonPressedBlock(self, indexPath);
- }
-
- if ([self.delegate respondsToSelector:@selector(actionSheet:pressedButtonAtIndexPath:)]) {
- [self.delegate actionSheet:self pressedButtonAtIndexPath:indexPath];
- }
- }
- #pragma mark Layout
- - (void)layoutSheetForFrame:(CGRect)frame fitToRect:(BOOL)fitToRect initialSetUp:(BOOL)initial continuous:(BOOL)continuous {
- if (continuous) {
- frame.size.width = kFixedWidthContinuous;
- }
-
- CGFloat spacing = 2.0f*kSpacing;
-
- CGFloat width = CGRectGetWidth(frame);
-
- // if (!continuous) {
- // width -= 2.0f*spacing;
- // }
-
- CGFloat height = (continuous ? 0.0f : spacing);
-
- for (JGActionSheetSection *section in self.sections) {
- if (initial) {
- [section setUpForContinuous:continuous];
- }
-
- CGRect f = [section layoutForWidth:width];
-
- f.origin.y = height;
-
- // if (!continuous) {
- f.origin.x = 0;
- // }
-
- section.frame = f;
-
- height += CGRectGetHeight(f)+spacing;
- }
-
- // if (continuous) {
- height -= spacing;
- // }
-
- _scrollView.contentSize = (CGSize){CGRectGetWidth(frame), height};
-
- if (!fitToRect && !continuous) {
- frame.size.height = CGRectGetHeight(_targetView.bounds)-CGRectGetMinY(frame);
- }
-
- if (height > CGRectGetHeight(frame)) {
- _scrollViewHost.frame = frame;
- }
- else {
- CGFloat finalY = 0.0f;
-
- if (fitToRect) {
- finalY = CGRectGetMaxY(frame)-height;
- }
- else if (continuous) {
- finalY = CGRectGetMinY(frame);
- }
- else {
- finalY = CGRectGetMinY(frame)+(CGRectGetHeight(frame)-height)/2.0f;
- }
-
- _scrollViewHost.frame = (CGRect){{CGRectGetMinX(frame), finalY}, _scrollView.contentSize};
- }
-
- _finalContentFrame = _scrollViewHost.frame;
-
- _scrollView.frame = _scrollViewHost.bounds;
-
- [_scrollView scrollRectToVisible:(CGRect){{0.0f, _scrollView.contentSize.height-1.0f}, {1.0f, 1.0f}} animated:NO];
- }
- - (void)layoutForVisible:(BOOL)visible {
- UIView *viewToModify = _scrollViewHost;
-
- if (visible) {
- self.backgroundColor = _realBGColor;
-
- if (iPad) {
- viewToModify.alpha = 1.0f;
- _arrowView.alpha = 1.0f;
- }
- else {
- viewToModify.frame = _finalContentFrame;
- }
- }
- else {
- super.backgroundColor = [UIColor clearColor];
-
- if (iPad) {
- viewToModify.alpha = 0.0f;
- _arrowView.alpha = 0.0f;
- }
- else {
- viewToModify.frame = (CGRect){{viewToModify.frame.origin.x, CGRectGetHeight(_targetView.bounds)}, _scrollView.contentSize};
- }
- }
- }
- #pragma mark Showing
- - (void)showInView:(UIView *)view animated:(BOOL)animated {
- NSAssert(!self.visible, @"Action Sheet is already visisble!");
-
- [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
-
- _targetView = view;
-
- [self layoutSheetInitial:YES];
-
- if ([self.delegate respondsToSelector:@selector(actionSheetWillPresent:)]) {
- [self.delegate actionSheetWillPresent:self];
- }
-
- void (^completion)(void) = ^{
- [[UIApplication sharedApplication] endIgnoringInteractionEvents];
-
- if ([self.delegate respondsToSelector:@selector(actionSheetDidPresent:)]) {
- [self.delegate actionSheetDidPresent:self];
- }
- };
-
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
-
- [self layoutForVisible:!animated];
-
- [_targetView addSubview:self];
-
- if (!animated) {
- completion();
- }
- else {
- CGFloat duration = (iPad ? 0.3f : kAnimationDurationForSectionCount(self.sections.count));
-
- [UIView animateWithDuration:duration animations:^{
- [self layoutForVisible:YES];
- } completion:^(BOOL finished) {
- completion();
- }];
- }
- }
- - (void)layoutSheetInitial:(BOOL)initial {
- self.frame = _targetView.bounds;
-
- _scrollViewHost.layer.cornerRadius = 0.0f;
- _scrollViewHost.layer.shadowOpacity = 0.0f;
- _scrollViewHost.backgroundColor = [UIColor clearColor];
-
- CGRect frame = self.frame;
-
- if (iPad) {
- CGFloat fixedWidth = kFixedWidth;
-
- frame.origin.x = (CGRectGetWidth(frame)-fixedWidth)/2.0f;
-
- frame.size.width = fixedWidth;
- }
-
- frame = UIEdgeInsetsInsetRect(frame, self.insets);
-
- [self layoutSheetForFrame:frame fitToRect:!iPad initialSetUp:initial continuous:NO];
- }
- #pragma mark Showing From Point
- - (void)showFromPoint:(CGPoint)point inView:(UIView *)view arrowDirection:(JGActionSheetArrowDirection)arrowDirection animated:(BOOL)animated {
- NSAssert(!self.visible, @"Action Sheet is already visisble!");
-
- if (!iPad) {
- return [self showInView:view animated:animated];
- }
-
- [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
-
- _targetView = view;
-
- [self moveToPoint:point arrowDirection:arrowDirection animated:NO];
-
- if ([self.delegate respondsToSelector:@selector(actionSheetWillPresent:)]) {
- [self.delegate actionSheetWillPresent:self];
- }
-
- void (^completion)(void) = ^{
- [[UIApplication sharedApplication] endIgnoringInteractionEvents];
-
- if ([self.delegate respondsToSelector:@selector(actionSheetDidPresent:)]) {
- [self.delegate actionSheetDidPresent:self];
- }
- };
-
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
-
- [self layoutForVisible:!animated];
-
- [_targetView addSubview:self];
-
- if (!animated) {
- completion();
- }
- else {
- CGFloat duration = 0.3f;
-
- [UIView animateWithDuration:duration animations:^{
- [self layoutForVisible:YES];
- } completion:^(BOOL finished) {
- completion();
- }];
- }
- }
- - (void)moveToPoint:(CGPoint)point arrowDirection:(JGActionSheetArrowDirection)arrowDirection animated:(BOOL)animated {
- if (!iPad) {
- return;
- }
-
- [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
-
- disableCustomEasing = YES;
-
- NSAssert(self.visible, @"Action Sheet requires to be visible in order to move the anchor point!");
-
- void (^changes)(void) = ^{
- self.frame = _targetView.bounds;
-
- CGRect finalFrame = CGRectZero;
-
- CGFloat arrowHeight = kArrowHeight;
-
- CGFloat spacing = kSpacing;
-
- if (arrowDirection == JGActionSheetArrowDirectionRight) {
- finalFrame.size.width = point.x-arrowHeight;
- finalFrame.size.height = CGRectGetHeight(_targetView.bounds);
- }
- else if (arrowDirection == JGActionSheetArrowDirectionLeft) {
- finalFrame.size.width = CGRectGetWidth(_targetView.bounds)-point.x-arrowHeight;
- finalFrame.size.height = CGRectGetHeight(_targetView.bounds);
- finalFrame.origin.x = point.x+arrowHeight;
- }
- else if (arrowDirection == JGActionSheetArrowDirectionTop) {
- finalFrame.size.width = CGRectGetWidth(_targetView.bounds);
- finalFrame.size.height = CGRectGetHeight(_targetView.bounds)-point.y-arrowHeight;
- finalFrame.origin.y = point.y+arrowHeight;
- }
- else if (arrowDirection == JGActionSheetArrowDirectionBottom) {
- finalFrame.size.width = CGRectGetWidth(_targetView.bounds);
- finalFrame.size.height = point.y-arrowHeight;
- }
- else {
- @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"Invalid arrow direction" userInfo:nil];
- }
-
- finalFrame.origin.x += spacing;
- finalFrame.origin.y += spacing;
- finalFrame.size.height -= spacing*2.0f;
- finalFrame.size.width -= spacing*2.0f;
-
- finalFrame = UIEdgeInsetsInsetRect(finalFrame, self.insets);
-
- _scrollViewHost.backgroundColor = [UIColor whiteColor];
-
- _scrollViewHost.layer.cornerRadius = kHostsCornerRadius;
-
- _scrollViewHost.layer.shadowColor = [UIColor blackColor].CGColor;
- _scrollViewHost.layer.shadowOffset = CGSizeZero;
- _scrollViewHost.layer.shadowRadius = kShadowRadius;
- _scrollViewHost.layer.shadowOpacity = kShadowOpacity;
-
- [self layoutSheetForFrame:finalFrame fitToRect:NO initialSetUp:YES continuous:YES];
-
- [self anchorSheetAtPoint:point withArrowDirection:arrowDirection availableFrame:finalFrame];
- };
-
- void (^completion)(void) = ^{
- [[UIApplication sharedApplication] endIgnoringInteractionEvents];
- };
-
- if (animated) {
- [UIView animateWithDuration:0.3 animations:changes completion:^(BOOL finished) {
- completion();
- }];
- }
- else {
- changes();
- completion();
- }
-
- disableCustomEasing = NO;
- }
- - (void)anchorSheetAtPoint:(CGPoint)point withArrowDirection:(JGActionSheetArrowDirection)arrowDirection availableFrame:(CGRect)frame {
- _anchoredAtPoint = YES;
- _anchorPoint = point;
- _anchoredArrowDirection = arrowDirection;
-
- CGRect finalFrame = _scrollViewHost.frame;
-
- CGFloat arrowHeight = kArrowHeight;
- CGFloat arrrowBaseWidth = kArrowBaseWidth;
-
- BOOL leftOrRight = (arrowDirection == JGActionSheetArrowDirectionLeft || arrowDirection == JGActionSheetArrowDirectionRight);
-
- CGRect arrowFrame = (CGRect){CGPointZero, {(leftOrRight ? arrowHeight : arrrowBaseWidth), (leftOrRight ? arrrowBaseWidth : arrowHeight)}};
-
- if (arrowDirection == JGActionSheetArrowDirectionRight) {
- arrowFrame.origin.x = point.x-arrowHeight;
- arrowFrame.origin.y = point.y-arrrowBaseWidth/2.0f;
-
- finalFrame.origin.x = point.x-CGRectGetWidth(finalFrame)-arrowHeight;
- }
- else if (arrowDirection == JGActionSheetArrowDirectionLeft) {
- arrowFrame.origin.x = point.x;
- arrowFrame.origin.y = point.y-arrrowBaseWidth/2.0f;
-
- finalFrame.origin.x = point.x+arrowHeight;
- }
- else if (arrowDirection == JGActionSheetArrowDirectionTop) {
- arrowFrame.origin.x = point.x-arrrowBaseWidth/2.0f;
- arrowFrame.origin.y = point.y;
-
- finalFrame.origin.y = point.y+arrowHeight;
- }
- else if (arrowDirection == JGActionSheetArrowDirectionBottom) {
- arrowFrame.origin.x = point.x-arrrowBaseWidth/2.0f;
- arrowFrame.origin.y = point.y-arrowHeight;
-
- finalFrame.origin.y = point.y-CGRectGetHeight(finalFrame)-arrowHeight;
- }
-
- if (leftOrRight) {
- finalFrame.origin.y = MIN(MAX(CGRectGetMaxY(frame)-CGRectGetHeight(finalFrame), CGRectGetMaxY(arrowFrame)-CGRectGetHeight(finalFrame)+kHostsCornerRadius), MIN(MAX(CGRectGetMinY(frame), point.y-CGRectGetHeight(finalFrame)/2.0f), CGRectGetMinY(arrowFrame)-kHostsCornerRadius));
- }
- else {
- finalFrame.origin.x = MIN(MAX(MIN(CGRectGetMinX(frame), CGRectGetMinX(arrowFrame)-kHostsCornerRadius), point.x-CGRectGetWidth(finalFrame)/2.0f), MAX(CGRectGetMaxX(frame)-CGRectGetWidth(finalFrame), CGRectGetMaxX(arrowFrame)+kHostsCornerRadius-CGRectGetWidth(finalFrame)));
- }
-
- if (!_arrowView) {
- _arrowView = [[JGActionSheetTriangle alloc] init];
- [self addSubview:_arrowView];
- }
-
- [_arrowView setFrame:arrowFrame arrowDirection:arrowDirection];
-
- if (!CGRectContainsRect(_targetView.bounds, finalFrame) || !CGRectContainsRect(_targetView.bounds, arrowFrame)) {
- NSLog(@"WARNING: Action sheet does not fit within view bounds! Select a different arrow direction or provide a different anchor point!");
- }
-
- _scrollViewHost.frame = finalFrame;
- }
- #pragma mark Dismissal
- - (void)dismissAnimated:(BOOL)animated {
- NSAssert(self.visible, @"Action Sheet requires to be visible in order to dismiss!");
-
- [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
-
- void (^completion)(void) = ^{
- [_arrowView removeFromSuperview];
- _arrowView = nil;
-
- _targetView = nil;
-
- [self removeFromSuperview];
-
- _anchoredAtPoint = NO;
- _anchoredArrowDirection = 0;
- _anchorPoint = CGPointZero;
-
- [[NSNotificationCenter defaultCenter] removeObserver:self];
-
- [[UIApplication sharedApplication] endIgnoringInteractionEvents];
-
- if ([self.delegate respondsToSelector:@selector(actionSheetDidDismiss:)]) {
- [self.delegate actionSheetDidDismiss:self];
- }
- };
-
- if ([self.delegate respondsToSelector:@selector(actionSheetWillDismiss:)]) {
- [self.delegate actionSheetWillDismiss:self];
- }
-
- if (animated) {
- CGFloat duration = 0.0f;
-
- if (iPad) {
- duration = 0.3f;
- }
- else {
- duration = kAnimationDurationForSectionCount(self.sections.count);
- }
-
- [UIView animateWithDuration:duration animations:^{
- [self layoutForVisible:NO];
- } completion:^(BOOL finished) {
- completion();
- }];
- }
- else {
- [self layoutForVisible:NO];
-
- completion();
- }
- }
- #pragma mark Visibility
- - (BOOL)isVisible {
- return (_targetView != nil);
- }
- @end
|