core data UIColor转换为 自定义数据类型 (其他类型数据 转换同理)

转载说明:(谢谢)

http://blog.csdn.net/a21064346/article/details/7792074

点击打开链接

不好意思,因为上传这段代码之后,就忙着写项目,当时也没有注意到代码不好复制什么的。

现在过来补充说明一下:下面这个其实就是自己去 写一个NSValueTransformer的类

它的思想是 将coredata关键字的属性 设置为tansformable的type,这样你才好用其他格式的来进行替换

装载它的容器 是一个NSData的数据格式。下面是例子

对于其他类型的数据,你也可以这样用。比如 数组类型的数据。

#import <Foundation/Foundation.h>

@interface UIColorRGBValueTransformer : NSValueTransformer

@end


#import "UIColorRGBValueTransformer.h"

@implementation UIColorRGBValueTransformer

+ (Class)transformedValueClass
{
    return [NSData class];
}

+ (BOOL)allowsReverseTransformation
{
    return YES;
}

- (id)transformedValue:(id)value
{
    UIColor* color = value;
    const CGFloat* components = CGColorGetComponents(color.CGColor);
    NSString* colorAsString = [NSString stringWithFormat:@"%f,%f,%f,%f", components[0], components[1], components[2], components[3]];
    return [colorAsString dataUsingEncoding:NSUTF8StringEncoding];
}

- (id)reverseTransformedValue:(id)value
{
    NSString* colorAsString = [[[NSString alloc] initWithData:value encoding:NSUTF8StringEncoding] autorelease];
    NSArray* components = [colorAsString componentsSeparatedByString:@","];
    CGFloat r = [[components objectAtIndex:0] floatValue];
    CGFloat g = [[components objectAtIndex:1] floatValue];
    CGFloat b = [[components objectAtIndex:2] floatValue];
    CGFloat a = [[components objectAtIndex:3] floatValue];

    return [UIColor colorWithRed:r green:g blue:b alpha:a];
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
自定义 UIActivityIndicatorView,您需要遵循以下步骤: 1. 创建一个新的 UIView,并将其大小设置为您想要的指示器的大小。 2. 在 UIView 中添加一个 CAShapeLayer,并设置其填充颜色和线宽。 3. 使用 CABasicAnimation 创建旋转动画,并将其添加到 CAShapeLayer 上。 4. 将 UIView 添加到您的视图层次结构中,并在需要时启动动画。 下面是一些示例代码,演示如何创建一个自定义的 UIActivityIndicatorView: ```swift class CustomActivityIndicatorView: UIView { private let shapeLayer = CAShapeLayer() override init(frame: CGRect) { super.init(frame: frame) configure() } required init?(coder: NSCoder) { super.init(coder: coder) configure() } private func configure() { shapeLayer.fillColor = UIColor.white.cgColor shapeLayer.strokeColor = UIColor.gray.cgColor shapeLayer.lineWidth = 3.0 shapeLayer.lineCap = .round shapeLayer.path = UIBezierPath(arcCenter: CGPoint(x: bounds.midX, y: bounds.midY), radius: bounds.width / 2 - shapeLayer.lineWidth / 2, startAngle: 0, endAngle: .pi * 2, clockwise: true).cgPath layer.addSublayer(shapeLayer) } func startAnimating() { let animation = CABasicAnimation(keyPath: "transform.rotation.z") animation.fromValue = 0 animation.toValue = CGFloat.pi * 2 animation.duration = 1 animation.repeatCount = .infinity shapeLayer.add(animation, forKey: "rotation") } func stopAnimating() { shapeLayer.removeAnimation(forKey: "rotation") } } ``` 在您的视图控制器中,可以像这样使用它: ```swift let activityIndicator = CustomActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 50, height: 50)) activityIndicator.center = view.center view.addSubview(activityIndicator) activityIndicator.startAnimating() // 在一段时间后停止动画 DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) { activityIndicator.stopAnimating() } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值