给category添加基本数据类型属性

说明

通常,我们添加属性都是通过对象扩展来实现的,其实,我们也可以用runtime来添加基本数据类型的属性

源码

//
// UIView+AnimationProperty.h
// RuntimeCategory
//
// Created by YouXianMing on 15/7/8.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> @interface UIView (AnimationProperty) @property (nonatomic) CGFloat scale; @end
//
// UIView+AnimationProperty.m
// RuntimeCategory
//
// Created by YouXianMing on 15/7/8.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import "UIView+AnimationProperty.h"
#import <objc/runtime.h> @implementation UIView (AnimationProperty) NSString * const _recognizerScale = @"_recognizerScale";
- (void)setScale:(CGFloat)scale { objc_setAssociatedObject(self, (__bridge const void *)(_recognizerScale), @(scale), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
self.transform = CGAffineTransformMakeScale(scale, scale);
}
- (CGFloat)scale { NSNumber *scaleValue = objc_getAssociatedObject(self, (__bridge const void *)(_recognizerScale));
return scaleValue.floatValue;
} @end
//
// ViewController.m
// RuntimeCategory
//
// Created by YouXianMing on 15/7/8.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "UIView+AnimationProperty.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView]; UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
greenView.backgroundColor = [UIColor greenColor];
greenView.scale = 0.5f;
[self.view addSubview:greenView]; } @end

分析

给category添加基本数据类型属性的更多相关文章

  1. 【原】iOS动态性(一):动态添加属性的方法——关联(e.g. 向Category添加属性)

    想到要如何为所有的对象增加实例变量吗?我们知道,使用Category可以很方便地为现有的类增加方法,但却无法直接增加实例变量.不过从Mac OS X v10.6开始,系统提供了Associative ...

  2. 用Objective-C的Category特性添加类的属性

    http://www.cnblogs.com/wupher/archive/2013/01/05/2845338.html Category是Objective-C中常用的语法特性,通过它可以很方便的 ...

  3. iOS动态性:动态添加属性的方法——关联(e.g. 向Category添加属性)

    想到要如何为所有的对象增加实例变量吗?我们知道,使用Category可以很方便地为现有的类增加方法,但却无法直接增加实例变量.不过从Mac OS X v10.6开始,系统提供了Associative ...

  4. iOS Category 添加属性实现原理 - 关联对象

    iOS Category 添加属性实现原理 - 关联对象 RunTime为Category动态关联对象 使用RunTime给系统的类添加属性,首先需要了解对象与属性的关系.对象一开始初始化的时候其属性 ...

  5. category添加属性

    category添加属性 面试题 Category的实现原理,以及Category为什么只能加方法不能加属性. Category中有load方法吗?load方法是什么时候调用的?load 方法能继承吗 ...

  6. ASP.NET MVC系列:为已有模型添加新的属性

    在模型类Movie中添加一个新的属性Rating

  7. 30.怎样在Swift中添加运行时属性?

    和OC一样,Swift中也可以添加运行时属性.下面将提供一个完整的例子,演示如何给按钮点击事件添加运行时属性. 1.示例 import UIKit var s_GofButtonTouchDownKe ...

  8. JavaScript中对象数组,如何给对象添加一个新属性

    var a =[{name: 'Tom',age:20},{name: 'Tom2',age:22}] 现在给a数组中的第一个对象添加性别属性 a[0]['gender']='women' a[0][ ...

  9. JavaScript Dom基础-9-Dom查找方法; 设置DOM元素的样式; innerHTML属性的应用; className属性的应用; DOM元素上添加删除获取属性;

    JavaScript Dom基础 学习目标 1.掌握基本的Dom查找方法 domcument.getElementById() Domcument.getElementBy TagName() 2.掌 ...

随机推荐

  1. Spring Boot 不使用默认的 parent,改用自己的项目的 parent

    在初学spring boot时,官方示例中,都是让我们继承一个spring的 spring-boot-starter-parent 这个parent: <parent> <group ...

  2. C#语法之Linq查询基础二

    上篇C#语法之Linq查询基础一基本把Linq介绍了一下,这篇主要是列举下它的几个常见用法. 在用之前先准备些数据,新建了两个类Student.Score,并通过静态方法提供数据. using Sys ...

  3. 008.在C#中,显式接口VS隐式接口

    原文http://www.codeproject.com/Articles/1000374/Explicit-Interface-VS-Implicit-Interface-in-Csharp (At ...

  4. MVC 之var与dynamic

    如果你用MVC写过程序,那么你应该知道ViewBag这个用于前后台的数据传递工具,那么你是否对ViewBag的用法感到过疑惑呢? ViewBag.Mode1l=new object(); ViewBa ...

  5. [日常] GO语言圣经-并发获取多个URL

    go语言圣经-并发获取多个URL 1.GO最新奇的特性就是对并发编程的支持,goroutine和channel 2.goroutine是一种函数的并发执行方式,而channel是用来在goroutin ...

  6. STS的安装教程-鹏鹏

    STS全称Spring Tools Suite. 简介:Spring Tools Suite (STS)其实就是一个被包装过的Eclipse,主要用于快速的开发Spring项目,我们不用再去编辑繁琐的 ...

  7. java工具类-交易码

    /** * 功能描述:交易码 * */ public class TransCode { //登录 public static final String LOGIN="A001"; ...

  8. 使用AutoFac组织多项目应用程序

    较复杂的应用程序都是由多个项目组织成的,项目可以划分成程序集(Assemblies)和宿主(Hosts),也就是应用程序的入口.      Assemblies 通常是常见的类库项目,包括可以重用的功 ...

  9. Django基础必备三神装(HttpResponse、render,、redirect)

    在使用三神装的时候,首先当然是得要导入它们: from django.shortcuts import HttpResponse, render, redirect 1.HttpResponse 它是 ...

  10. 小tip:FireFox下文本框/域百分比padding bug解决——张鑫旭

    一.问题描述 我是流体布局控,经常会遇到文本框以及文本域宽度100%自适应显示的情况. 如下效果图: 在窄屏下,上面的文本框宽度也要跟着外部宽度变小. 难点对于文本框或者文本域,光标最好距离左侧边缘有 ...