iOS 常用代码片段

2024-02-28  本文已影响0人  蓝天白云_Sam

时间操作

时间加减:当前时间减去18年

NSDate *cur = [NSDate date];
NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *dateComponents = [calendar components:NSCalendarUnitYear fromDate:cur];
[dateComponents setYear:-18];
 NSDate *birthday = [calendar dateByAddingComponents:dateComponents toDate:cur options:0];

时间格式化

NSDate *cur = [NSDate date];
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
NSString *dateString = [fmt stringFromDate:];

获取时间年月日

NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:birthday];
NSInteger birthYear = dateComponents.year;
NSInteger birthMon = dateComponents.month;
NSInteger birthDay = dateComponents.day;

UI特效

渐变色

@interface WSGradientView : UIView

@property (nonatomic, strong) CAGradientLayer *gradientLayer;

@end

#import "WSGradientView.h"


@implementation WSGradientView

+ (Class)layerClass
{
    return [CAGradientLayer class];
}

- (CAGradientLayer *)gradientLayer
{
    return (CAGradientLayer *)[self layer];
}

@end

带有渐变色的蒙版

         UIView *maskView = WSGradientView.new;
        maskView.gradientLayer.colors = @[ (__bridge id)[UIColor.redColor colorWithAlphaComponent:1].CGColor,
                                           (__bridge id)[UIColor.redColor colorWithAlphaComponent:1].CGColor,
                                           (__bridge id)[UIColor.redColor colorWithAlphaComponent:1].CGColor,
                                           (__bridge id)[UIColor.redColor colorWithAlphaComponent:0.0].CGColor ];
        maskView.gradientLayer.startPoint = CGPointMake(0, 0);
        maskView.gradientLayer.endPoint = CGPointMake(0, 1);
        maskView.gradientLayer.locations = @[ @(0), @(0.1), @(0.77), @(1) ];
        self.view.maskView = maskView;
        maskView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 232);
上一篇下一篇

猜你喜欢

热点阅读