UI基础第二天(知识点)

本文深入探讨了iOS开发中的Swift编程语言,包括基础语法、常用框架、性能优化及实战案例解析,帮助开发者提高开发效率并解决常见问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、UIView的动画播放的类方法

[UIView animateWithDuration:(NSTimeInterval) animations:^(void)animations];

animateWithDuration:表示播放时间,单位为秒

animations:表示要播放的动画内容

    //以两秒的速度,顺时针旋转button M_PI_2度
    [UIView animateWithDuration:2.0 animations:^{
        self.headBtn.transform = CGAffineTransformRotate(self.button.transform, M_PI_2);
    }];

二、控件的frame值的修改

1、间接修改

    //取出button的frame
    CGRect frame = self.button.frame;
    //调整frame
    frame.origin.x = 10;
    frame.origin.y = 10;
    frame.size.width = 10;
    frame.size.height = 10;
    //修改button的frame 
    self.button.frame = frame;


2、强制转换

    //把值强制转换成CGRect类型,在进行赋值
    self.button.frame = (CGRect){10,10,10,10};
3、CGRectMake函数

    //使用CGRectMake函数进行赋值
    self.button.frame = CGRectMake(10,10,10,10);

三、懒加载(延迟加载)

懒加载:使用的时候才加载,效率低,节约内存;

实现懒加载:重写getter方法。

优点:代码的可读性更强,不用把加载数据写在viewDidLoad方法中;

//数据
@property (nonatomic, strong) NSArray* dataArray;

//懒加载,使用时加载
-(NSArray*) dataArray{
    //判断是否已经实例化,没有实例化才进行加载数据
    if (_dataArray==nil) {
        //NSBundle:应用所在的文件夹
        NSBundle* bundle = [NSBundle mainBundle];
        //获取plist的全路径
        NSString* path = [bundle pathForResource:@"imageData" ofType:@"plist"];
        //根据路径取出数组数据
        _dataArray = [NSArray arrayWithContentsOfFile:path];
    }
    return _dataArray;
}

四、UIImage的2种加载方式

1、根据图片名称创建图片
  • 使用imageNamed创建图片,有缓存,程序运行时图片会一直在内存中,把图片放在 Images.xcassets 里,就只能通过imageNamed,放在目录里的图片也可通过imageNamed创建。

       UIImage * image = [UIImage imageNamed:imageName];

2、根据图片的全路径创建图片
  • 使用imageWithContentsOfFile:或initWithContentsOfFile:创建图片,无缓存,图片所占用的内存会在一些特定操作后被清除
  • 类NSBundle表示应用所在文件,pathForResource:ofType:方法是获取文件的全路径,不能获取Images.xcassets里图片的全路径。

       //NSBundle 应用所在文件  
       NSBundle *bundle = [NSBundle mainBundle];
       //获取应用所在文件下的cat.jpg的全路径
       NSString * path = [bundle pathForResource:@"cat" ofType:@"jpg"];        
       //很据图片的全路径创建图片,imageWithContentsOfFile不带缓存
       UIImage * image = [UIImage  imageWithContentsOfFile:path];

五、Xcode调试方法

1、条件断点:在代码的行号右键单击,设置该行的断点条件

2、添加异常提示:切换到Show the Breakpoint navigator,在左下脚有一个加号,点击添加Add Exception Breakpoint

六、代码实现控件

1、UIButton(按钮)

    UIButton *button = [[UIButton alloc]init];
    [self.view addSubview:button];

    //设置尺寸
    button.frame = CGRectMake(30, 150, 300, 100);
    //设置背景颜色
    [button setBackgroundColor:[UIColor blueColor]];
    //设置普通状态下的标题
    [button setTitle:@"按 钮" forState:UIControlStateNormal];
    //设置标题字体大小
    button.titleLabel.font = [UIFont boldSystemFontOfSize:30];
    //设置普通状态标题的颜色
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //设置标题阴影大小
    button.titleLabel.shadowOffset = CGSizeMake(3.0, 3.0);
    //设置阴影的颜色
    [button setTitleShadowColor:[UIColor redColor] forState:UIControlStateNormal];
    //在按钮中追加图片,只有按钮的类型为UIButtonTypeRoundedRect和UIButtonTypeCustom时有效
    [button setImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];
    //调整按钮的边间距
    UIEdgeInsets insets;
    insets.top = insets.bottom = insets.left = insets.right = 15;
    button.contentEdgeInsets = insets;
    //添加按钮的响应方法
    [button addTarget:self action:@selector(helloWold) forControlEvents:UIControlEventTouchDragInside];
    //根据内容对按钮的大小进行调整
    [button sizeToFit];
    //设置按钮的透明度
    button.alpha = 0.5; 
2、UILabel(标签)

    UILabel *label = [[UILabel alloc]init];
    [self.view addSubview:label];
    
    //调整标签的大小,与屏幕一样大小
    label.frame = self.view.bounds;
    //设置背景颜色
    label.backgroundColor = [UIColor greenColor];
    //设置标签文字
    label.text = @"标签标签标签标签标签标签标签标签标签";
    //设置文字的颜色
    label.textColor = [UIColor yellowColor];
    //设置文字大小
    label.font = [UIFont systemFontOfSize:58.0];
    //设置阴影的颜色
    label.shadowColor = [UIColor redColor];
    //设置阴影的位置
    label.shadowOffset = CGSizeMake(3, 3);
    //设置文字居中
    label.textAlignment = NSTextAlignmentCenter;
    //设置多行显示,0表示当标签足够大时,自动调整行数;大于0时,1表示一行,2表示两行,3表示是三行...
    label.numberOfLines = 0;
    //设置换多行时的省略方式
    label.lineBreakMode = NSLineBreakByTruncatingHead;
    
    //设置标签自动调整宽和高
    //label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    //字体自动调整大小,达到能在屏幕上全部显示
    //label.adjustsFontSizeToFitWidth = YES;
    //限制字体能缩小的最小值
    //label.minimumScaleFactor = 13;

3、UITextField(文本输入框)

    UITextField *TextField = [[UITextField alloc]init];
    [self.view addSubview:TextField];
    
    //设置尺寸
    TextField.frame = CGRectMake(40,200, 280, 30);
    //设置输入框的样式
    TextField.borderStyle = UITextBorderStyleRoundedRect;
    //设置输入框的背景颜色
    TextField.backgroundColor = [UIColor whiteColor];
    //设置输入框的提示信息
    TextField.placeholder = @"请输入文字";
    //设置默认文本
    //TextField.text = @"文本输入框";
    //设置字体颜色
    TextField.textColor = [UIColor blueColor];
    //设置字体大小
    TextField.font = [UIFont systemFontOfSize:24];
    //设置文本的居中对齐
    TextField.textAlignment = NSTextAlignmentCenter;
    //追加文本框的清空按钮
    TextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    //设置第一响应者,打开键盘
    [TextField becomeFirstResponder];
    //失去第一响应者,关闭键盘
    [TextField resignFirstResponder];
    //设置输入框不可编辑
    //[TextField setEnabled:NO];
    //追加响应事件
    [TextField addTarget:self action:@selector(eventName) forControlEvents:UIControlEventTouchDragInside];

4、UIImageView(图片显示)
  • UIImageView的创建

    UIImageView *ImageView = [[UIImageView alloc]init];
    [self.view addSubview:ImageView];
    
    //设置大小
    ImageView.frame = CGRectMake(80, 200, 200, 200);
    //添加图片
    ImageView.image = [UIImage imageNamed:@"buttongreen"];

  • UIImageView的动画属性和相关方法
    @property(nonatomic,copy) NSArray *animationImages;    //需要播放的序列帧图片数组(里面都是UIImage对象,会按顺序显示里面的图片)   
    @property(nonatomic) NSTimeInterval animationDuration;    //帧动画的持续时间    
    @property(nonatomic) NSInteger animationRepeatCount;    //帧动画的执行次数(默认是无限循环)    
    - (void)startAnimating;    //开始执行帧动画  
    - (void)stopAnimating;    //停止执行帧动画   
    - (BOOL)isAnimating;    //是否正在执行帧动画

  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值