Light's Blog

The best or nothing.

iOS知识小集-180418

| Comments

UISlider 自定义thumb大小

需继承UISlider并重写- thumbRectForBounds:trackRect:value:方法。

1
2
3
4
5
6
- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value{
  CGFloat customWidth = 20;
  CGFloat customHeight = 20;
  bounds = [super thumbRectForBounds:bounds trackRect:rect value:value];
  return CGRectMake(bounds.origin.x, bounds.origin.y, customWidth, customHeight);
}

YYModel 自定义转换

NSDateNSNumber转换为例。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// JSON:
{
  "name":"Harry",
  "timestamp" : 1445534567
}

// Model:
@interface User
@property NSString *name;
@property NSDate *createdAt;
@end

@implementation User
- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {
    NSNumber *timestamp = dic[@"timestamp"];
    if (![timestamp isKindOfClass:[NSNumber class]]) return NO;
    _createdAt = [NSDate dateWithTimeIntervalSince1970:timestamp.floatValue];
    return YES;
}
- (BOOL)modelCustomTransformToDictionary:(NSMutableDictionary *)dic {
    if (!_createdAt) return NO;
    dic[@"timestamp"] = @(n.timeIntervalSince1970);
    return YES;
}
@end

Comments