天天看点

渐变动画、图片拉伸、copy、按钮、kvc、kvo渐变动画、图片拉伸、copy、按钮、kvc、kvo

渐变动画、图片拉伸、copy、按钮、kvc、kvo

标签(空格分隔): ios基础

渐变动画

  • 方式1:头尾式
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:];

/* 需要执行动画的代码 */

[UIView commitAnimations];
           
  • 方式2:block
[UIView animateWithDuration: delay: options:kNilOptions animations:^{
    /* 需要执行动画的代码 */
} completion:nil]

// 1s后,再执行动画(动画持续2s)
           

按钮

  • 自定义按钮:调整内部子控件的frame
    • 方式1:实现titleRectForContentRect:和imageRectForContentRect:方法,分别返回titleLabel和imageView的frame
    • 方式2:在layoutSubviews方法中设置

      -内边距

// 设置按钮内容的内边距(影响到imageView和titleLabel)
@property(nonatomic)          UIEdgeInsets contentEdgeInsets;
// 设置titleLabel的内边距(影响到titleLabel)
@property(nonatomic)          UIEdgeInsets titleEdgeInsets;
// 设置imageView的内边距(影响到imageView)
@property(nonatomic)          UIEdgeInsets imageEdgeInsets;
           

图片拉伸

  • ios5之前
// 只拉伸中间的1x1区域
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;
           
  • ios5开始
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets;
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode;
           

拷贝

-实现拷贝的方法有2个

-copy:返回不可变副本

- mutableCopy:返回可变副本

- 普通对象实现拷贝的步骤

- 遵守NSCopying协议

- 实现-copyWithZone:方法

- 创建新对象

- 给新对象的属性赋值

KVC

  • 全称:Key Value Coding(键值编码)
  • 赋值
// 能修改私有成员变量

- (void)setValue:(id)value forKey:(NSString *)key;
- (void)setValue:(id)value forKeyPath:(NSString *)keyPath;
- (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues;
           
  • 取值
//能取得私有成员变量的值(**这个很有用**)
- (id)valueForKey:(NSString *)key;
- (id)valueForKeyPath:(NSString *)keyPath;
- (NSDictionary *)dictionaryWithValuesForKeys:(NSArray *)keys;
           
  • 特殊的用法,包括计算books数组的count,属性最大值和平均值
NSLog(@"%@",[p1 valueForKeyPath:@"[email protected]"]);
    NSLog(@"%@",[p1 valueForKeyPath:@"[email protected]"]);
    NSLog(@"%@",[p1 valueForKeyPath:@"[email protected]"]);
           

KVO

  • 全称:Key Value Observing(键值监听)
  • 作用:监听模型的属性值改变
  • 步骤
    • 添加监听器
      // 利用b对象来监听a对象name属性的改变
      [a addObserver:b forKeyPath:@"name" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:@"test"];
                 
    • 取消监听
    [a removeObserver:b forKeyPath:@"name"];
               
    • 在监听器中实现监听方法
    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object      change:(NSDictionary *)change   context:(void *)context
        {
            NSLog(@"%@ %@ %@ %@", object, keyPath, change, context);
        }