自定义的ActionSheet,需的拿去

自定义的ActionSheet,需要的拿去

现在流行的一种ActionSheet,功能丰富 ,自定义的ActionSheet,实现非常好看的效果。



自定义的ActionSheet,需的拿去
 
 

自定义的ActionSheet,需的拿去
 
 

源码片段:

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];
}
源码下载地址:http://www.devstore.cn/code/info/94.html