您的位置:首页 > 产品设计 > UI/UE

UIView使用CALayer添加单独一边

2015-11-09 13:13 302 查看
#define kValidDirections [NSArray arrayWithObjects: @"top", @"bottom", @"left", @"right",nil]

- (void) insertLayertoView:(UIView*)sourceView WithRadius:(float)radius Color:(UIColor *)color Directions:(NSArray *)directions

{

    

    // Ignore duplicate direction

    NSMutableDictionary *directionDict = [[NSMutableDictionary alloc] init];

    for (NSString *direction in directions) [directionDict setObject:@"1" forKey:direction];

    

    for (NSString *direction in directionDict) {

        // Ignore invalid direction

        if ([kValidDirections containsObject:direction])

        {

            CAGradientLayer *shadow = [CAGradientLayer layer];

            

            if ([direction isEqualToString:@"top"]) {

                [shadow setStartPoint:CGPointMake(0.5, 0.0)];

                [shadow setEndPoint:CGPointMake(0.5, 1.0)];

                shadow.frame = CGRectMake(0, 0, sourceView.bounds.size.width, radius);

            }

            else if ([direction isEqualToString:@"bottom"])

            {

                [shadow setStartPoint:CGPointMake(0.5, 1.0)];

                [shadow setEndPoint:CGPointMake(0.5, 0.0)];

                shadow.frame = CGRectMake(0, sourceView.bounds.size.height - radius, sourceView.bounds.size.width, radius);

            } else if ([direction isEqualToString:@"left"])

            {

                shadow.frame = CGRectMake(0, 0, radius, sourceView.bounds.size.height);

                [shadow setStartPoint:CGPointMake(0.0, 0.5)];

                [shadow setEndPoint:CGPointMake(1.0, 0.5)];

            } else if ([direction isEqualToString:@"right"])

            {

                shadow.frame = CGRectMake(sourceView.bounds.size.width - radius, 0, radius, sourceView.bounds.size.height);

                [shadow setStartPoint:CGPointMake(1.0, 0.5)];

                [shadow setEndPoint:CGPointMake(0.0, 0.5)];

            }

            

            shadow.colors = [NSArray arrayWithObjects:(id)[color CGColor], (id)[[UIColor clearColor] CGColor], nil];

            [sourceView.layer insertSublayer:shadow atIndex:0];

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: