• UI基础UIView常见属性及方法


    1.NSBundle

    1> 一个NSBundle代表一个文件夹,利用NSBundle能访问对应的文件夹

    2> 利用mainBundle就可以访问软件资源包中的任何资源

    3> 模拟器应用程序的安装路径

    /Users/aplle/资源库/Application Support/iPhone Simulator/7.1/Applications

    2.UIImageView和UIButton

    1> 使用场合

    * UIImageView: 如果仅仅是显示图片,不需要监听图片的点击

    * UIButton: 既要显示图片,又要监听图片的点击

    2> 相同:能显示图片

    3> 不同点

    * UIButton能处理点击事件, UIImageView不能处理点击事件

    * UIButton既能显示图片, 又能显示文字

    * UIButton能同时显示两张图片

    * UIButton继承自UIControl, 因此默认就能处理事件

    * UIImageView继承自UIView, 因此默认就不能处理事件

    3.Xcode文档安装路径

    /Applications/Xcode.app/Contents/Developer/Documentation/DocSets

    4.Xcode模拟器安装路径

    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs

    5,UIView的常⻅见属性

    ➢ @property(nonatomic,readonly) UIView *superview;

    ➢ 获得⾃自⼰己的⽗父控件对象

    ➢ @property(nonatomic,readonly,copy) NSArray *subviews;

    ➢ 获得⾃自⼰己的所有⼦子控件对象

    ➢ @property(nonatomic) NSInteger tag;

    ➢ 控件的ID标识,⽗父控件可以通过tag来找到对应的⼦子控件

    ➢ @property(nonatomic) CGAffineTransform transform;

    ➢ 控件的形变属性(可以设置旋转⾓角度、⽐比例缩放、平移等属性)

    在OC中,通过transform属性可以修改对象的平移、缩放比例和旋转角度

    常用的创建transform结构体方法分两大类

    (1) 创建“基于控件初始位置”的形变

    CGAffineTransformMakeTranslation(平移)

    CGAffineTransformMakeScale(缩放)

    CGAffineTransformMakeRotation(旋转)

    (2) 创建“基于transform参数”的形变

    CGAffineTransformTranslate

    CGAffineTransformScale

    CGAffineTransformRotate

    补充:

    在OC中,所有跟角度相关的数值,都是弧度值,180° = M_PI

    正数表示顺时针旋转

    负数表示逆时针旋转

    提示:由于transform属性可以基于控件的上一次的状态进行叠加形变,例如,先旋转再平移。因此在实际动画开发中,当涉及位置、尺寸形变效果时,大多修改控件的transform属性,而不是frame、bounds、center 。

    @property(nonatomic) CGRect frame; 控件所在矩形框在⽗父控件中的位置和尺⼨寸(以⽗父控件的左上⾓角为坐标原点)

    @property(nonatomic) CGRect bounds; 控件所在矩形框的位置和尺⼨寸(以⾃自⼰己左上⾓角为坐标原点,所以bounds的xy⼀一般

                                                       为0)

    @property(nonatomic) CGPoint center;

    控件中点的位置(以⽗父控件的左上⾓角为坐标原点)

    6,UIView的常⻅见⽅方法

    ➢ - (void)addSubview:(UIView *)view;

    ➢ 添加⼀一个⼦子控件view

    ➢ - (void)removeFromSuperview;

    ➢ 从⽗父控件中移除

    ➢ - (UIView *)viewWithTag:(NSInteger)tag;

    ➢ 根据⼀一个tag标识找出对应的控件(⼀一般都是⼦子控件)

    7,动画的两种创建方式

     1)

      //开始动画

     [UIView beginAnimations:nil context:nil];

     // 执行动画内容

     self.headImageView.bounds=bounds;

     // 设置动画时间

     [UIView setAnimationDuration:2.0];

     // 提交动画

     [UIView commitAnimations];

     2) // 一般用这种方式

      UIView animateKeyframesWithDuration:<#(NSTimeInterval)#> delay:<#(NSTimeInterval)#> options:<#(UIViewKeyframeAnimationOptions)#> animations:<#^(void)animations#> completion:<#^(BOOL finished)completion#>

    8,汤姆猫中动画执行代码顺序

     // 1,设置图片数组

    self.tom.animationImages = images;

    // 2.设置播放次数(1次)

    self.tom.animationRepeatCount = 1;

    // 3.设置播放时间

    self.tom.animationDuration = images.count * 0.05;

    // 4,开始动画

    [self.tom startAnimating];

     性能问题

    // 加载图片

    // imageNamed: 有缓存(传入文件名)

    //        UIImage *image = [UIImage imageNamed:filename];

     解决办法

    // imageWithContentsOfFile: 没有缓存(传入文件的全路径)

    NSBundle *bundle = [NSBundle mainBundle];

    NSString *path = [bundle pathForResource:filename ofType:nil];

    UIImage *image = [UIImage imageWithContentsOfFile:path];

    动画放完1秒后清除内存

    CGFloat delay = self.tom.animationDuration + 1.0;

    [self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:delay];

    代码图片浏览器动态代码:

    模型类

    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    
    @interface LLPic : NSObject
    
    // 图片地址
    @property (nonatomic, copy) NSString *icon;
    
    // 图片
    @property (nonatomic, strong) UIImage *image;
    
    // 描述
    @property (nonatomic, copy) NSString *desc;
    
    + (instancetype)picWithDic:(NSDictionary *)dic;
    - (instancetype)initWithDic:(NSDictionary *)dic;
    
    + (NSArray *)array;
    
    @end
    #import "LLPic.h"
    
    @interface LLPic ()
    {
        UIImage *_imageABC;
    }
    @end
    
    @implementation LLPic
    
    + (NSArray *)array
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"images.plist" ofType:nil];
        
        NSArray *arrPic = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *arrPicMA = [[NSMutableArray alloc] initWithCapacity:arrPic.count];
        for (NSDictionary *dic in arrPic) {
            
            [arrPicMA addObject:[self picWithDic:dic]];
        }
        return arrPicMA;
    }
    
    - (UIImage *)image
    {
        if (!_imageABC) {
            _imageABC = [UIImage imageNamed:self.icon];
        }
        return _imageABC;
    }
    
    - (instancetype)initWithDic:(NSDictionary *)dic
    {
        if (self = [super init]) {
            
            [self setValuesForKeysWithDictionary:dic];
        }
        return self;
    }
    
    + (instancetype)picWithDic:(NSDictionary *)dic
    {
        return [[self alloc] initWithDic:dic];
    }
    
    @end

    controller

    #import "ViewController.h"
    #import "LLPic.h"
    @interface ViewController ()
    
    // 属性值必须改为strong,如果联系IBOutLet则为week
    @property (nonatomic, strong) NSArray *picView;
    @property (nonatomic, strong) UILabel *headLab;
    @property (nonatomic, strong) UIImageView *imageView;
    @property (nonatomic, strong) UIButton *leftBtn;
    @property (nonatomic, strong) UIButton *rightBtn;
    @property (nonatomic, strong) UILabel *footLab;
    @property (nonatomic, strong) UIButton *leftRotationBtn; // 左旋转
    @property (nonatomic, strong) UIButton *rightRotationBtn; // 右旋转
    // 计数器
    @property (nonatomic, assign) int index;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        self.view.backgroundColor = [UIColor purpleColor];
    
        [self loadPicView];
    }
    
    #pragma mark - 按钮点击事件
    // 左点击
    - (void)leftBtnClick
    {
        self.index--;
        [self loadPicView];
    }
    // 右点击
    - (void)rightBtnClick
    {
        self.index++;
        [self loadPicView];
    }
    
    
    // 按钮旋转事件
    - (void)rotationBtn:(UIButton *)btn
    {
        [UIView animateWithDuration:0.2 animations:^{
            if (btn.tag == 20) {
                self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, M_PI_4);
            } else if (btn.tag == 40){
                self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, -M_PI_4);
            }
            
        }];
    }
    
    #pragma mark - 展示界面
    - (void)loadPicView
    {
        // 1,拿出模型对象
        
        LLPic *pic = self.picView[self.index];
        
        self.headLab.text = [NSString stringWithFormat:@"%d/%ld", self.index+1, self.picView.count];
        self.imageView.image = [UIImage imageNamed:pic.icon];
        self.footLab.text = pic.desc;
        
        self.leftBtn.enabled = (self.index != 0);
        self.rightBtn.enabled = (self.index != (self.picView.count-1));
        
        // 2,加载按钮
        [self creatRoationBtn];
        
    }
    #pragma mark - 懒加载控件,代码可读性强,并且提高性能
    // headLab的加载
    - (UILabel *)headLab
    {
        if (!_headLab) {
            
            _headLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 30)];
            _headLab.textAlignment = NSTextAlignmentCenter;
            [self.view addSubview:_headLab];
            
        }
        return _headLab;
    }
    
    // imageView的加载
    - (UIImageView *)imageView
    {
        if (!_imageView) {
            
            _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(70, 70, 180, 180)];
            [self.view addSubview:_imageView];
        }
        return _imageView;
    }
    
    // leftBtn的加载
    - (UIButton *)leftBtn
    {
        if (!_leftBtn) {
            
            _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            _leftBtn.frame = CGRectMake(0,125, 40, 40);
            [_leftBtn setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal];
            [_leftBtn setBackgroundImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted];
            [_leftBtn setBackgroundImage:[UIImage imageNamed:@"left_disable"] forState:UIControlStateDisabled];
            
            // 按钮点击事件
            [_leftBtn addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:_leftBtn];
        }
        return _leftBtn;
    }
    
    // rightBtn的加载
    - (void)creatRoationBtn
    {
        _rightRotationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightRotationBtn.frame = CGRectMake(200,400,40, 40);
        [_rightRotationBtn setBackgroundImage:[UIImage imageNamed:@"right_rotate_normal"] forState:UIControlStateNormal];
        [_rightRotationBtn setBackgroundImage:[UIImage imageNamed:@"right_rotate_highlighted"] forState:UIControlStateHighlighted];
        _rightRotationBtn.tag = 20;
        [_rightRotationBtn addTarget:self action:@selector(rotationBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_rightRotationBtn];
        
        _leftRotationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _leftRotationBtn.frame = CGRectMake(100,400,40, 40);
        [_leftRotationBtn setBackgroundImage:[UIImage imageNamed:@"left_rotate_normal"] forState:UIControlStateNormal];
        [_leftRotationBtn setBackgroundImage:[UIImage imageNamed:@"left_rotate_highlighted"] forState:UIControlStateHighlighted];
        _leftRotationBtn.tag = 40;
        [_leftRotationBtn addTarget:self action:@selector(rotationBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_leftRotationBtn];
    
    }
    - (UIButton *)rightBtn
    {
        if (!_rightBtn) {
            
            _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            _rightBtn.frame = CGRectMake(280,125,40, 40);
            [_rightBtn setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal];
            [_rightBtn setBackgroundImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted];
            [_rightBtn setBackgroundImage:[UIImage imageNamed:@"right_disable"] forState:UIControlStateDisabled];
            
            [_rightBtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:_rightBtn];
        }
        return _rightBtn;
    
    }
    
    
    
    // 描述lab
    - (UILabel *)footLab
    {
        if (!_footLab) {
            
            _footLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 260, 300, 60)];
            _footLab.textAlignment = NSTextAlignmentCenter;
            _footLab.numberOfLines = 0;
            [self.view addSubview:_footLab];
            
        }
    
        return _footLab;
    }
    
    #pragma mark - 加载数据模型
    - (NSArray *)picView
    {
        if (!_picView) {
            
            _picView = [LLPic array];
        }
        return _picView;
    }
    
    @end

    完成展示:

  • 相关阅读:
    六大设计原则
    spring的文件上传应用
    web项目的分页
    自己尝试写web,网页上点击链接没反应,没有发出http请求。
    python3.6使用chardet模块总是报错ValueError: Expected a bytes object, not a unicode object
    chardet模块的安装:检查编码类型
    decode,encode的用法
    转:Unicode,UTF-8,UTF-16,UTF-32,gb2312,gbk区别
    js赋值运算符 && 自增自减
    js算数运算符
  • 原文地址:https://www.cnblogs.com/-boy/p/4106818.html
Copyright © 2020-2023  润新知