UI003:动画简单使用,动态添加控件

1.简单动画的2种实现方式:

系统会根据某个属性值的改编,自动形成动画。

  • 头尾式动画:
// 修改center坐标。
[UIView beginAnimations:nil context:nil]; // 1.开启动画
[UIView setAnimationDuration: 2]; // 2. 设置动画执行时间,单位s
// ******需要执行动画的代码******
self.btnIcon.center = centerPoint;
[UIView commitAnimations]; // 3. 提交动画
  • Block式动画:推荐使用
[UIView animateWithDuration:0.5  animations: ^{
    // 需要执行动画的代码
    self.btnIcon.frame = originFrame;
}];

通过以下属性可以修改控件的位置

  • frame.origin
  • center

通过以下属性,可以修改空间的尺寸

  • frame.size
  • bounds.size

2. UIKit坐标系

在UIKit中,坐标系的原点(0, 0)在左上角,x值向右正向延伸,y值向下正向延伸。
center是控件中心点的(x, y)坐标。

3. 动态添加控件。

// 当要显示一个界面时,首先创建这个界面对应的控制器,控制器创建好后,
// 接着创建控制器所管理的那个view,这个view加载完毕后,就开始执行下面的方法。
// 所以,只要viewDidLoad方法被执行了,就表示控制器所管理的view 创建好了。
- (void)viewDidLoad {
    [super viewDidLoad];
    // 1. 动态创建按钮
    UIButton *button = [[UIButton alloc] init];
    // UIButton *button =[Button butonWithType:UIButtonTypeCustom];
    // 2.设置按钮上的文字,文字颜色
    [button setTitle:@"点我吧" forState:UIControlStateNormal];
    [button setTitle:@"点击状态文字" forState:UIControlStateHighlighted];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
    
    // 3.设置不同状态的背景图
    UIImage *imgNormal = [UIImage imageNamed:@"btn_01"];
    UIImage *imgHighlighted = [UIImage imageNamed:@"btn_02"];
    [button setBackgroundImage:imgNormal forState:UIControlStateNormal];
    [button setBackgroundImage:imgHighlighted forState:UIControlStateHighlighted];
    // 设置按钮frame
    button.frame = CGRectMake(50, 50, 100, 100);
    // 4.为按钮注册单击事件
    [button addTarget:self  action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
       
    // 5.把按钮添加到控制器所管理的view中。
    [self.view addSubview:button];
}
-(void)buttonClick {
    NSLog(@"按钮被点击了");
}


离线文档安装路径:
路径1:
/Applications/Xcode.app/Contents/Developer/Documentation/DocSets
路径2:
/Users/tq/Library/Developer/Shared/Documentation/DocSets


transform实现平移,旋转效果 :

  • 利用transform属性可以修改位移,缩放,旋转。
    创建1个transrorm属性。
    CGAffineTransform CGAffineTransformMakeTranslation(CGFloat tx, CGFloat ty); // 平移,向上是负的值。向下是正值,原始值0.
    CGAffineTransform CGAffineTransformMakeScale(CGFloat sx, CFGloat sy); // 缩放
    CGAffineTransform CGAffineTransformMakeRotation(CGFloat angle); // 旋转:angle是弧度值。
    // 45°,对应 M_PI_4;

  • 在某个transform的基础上进行叠加。使用原来的transform值再位移。
    CGAffineTransform CGAffineTransformTranslation(CGAffineTransform t, CGFloat tx, CGFloat ty);
    CGAffineTransform CGAffineTransformScale(CGAffineTransform t, CGFloat sx, CFGloat sy);
    CGAffineTransform CGAffineTransformRotation(CGAffineTransform t, CGFloat angle);

清空之前设置的transform:回到原来的位置。
view.transform = CGAffineTransformIdentity;

-(IBAction) move {
    // CGAffineTransform transForm = self.btnIcon.transform;
    // self.btnIcon.transform = CGAffineTransformMakeTranslation(0, -50); // 基于原点,向上平移50
    self.btnIcon.transform = CGAffineTransformTranslation(self.btnIcon.transform, 0, 50); // 向下平移50
}
-(IBAction) scale {
    // self.btnIcon.transform = CGAffineTransformScale(0.5, 0.5); // 缩小
    self.btnIcon.transform = CGAffineTransformScale(self.btnIcon.transform, 1.5, 1.5); // 基于原来的值放大
}
-(IBAction) rotate {
    // 同时执行多个动画
    [UIView animateWithDuration:2  animations:^ {
        self.btnIcon.transform = CGAffineTransformScale(self.btnIcon.transform, 1.5, 1.5);
        self.btnIcon.transform = CGAffineTransformMakeRotation(self.btnIcon.transform, M_PI_4); 
        self.btnIcon.transform = CGAffineTransformTranslation(self.btnIcon.transform, 0, 50); 
    }];
}

UIView常见属性

@property(nonatomic, readonly) UIView *superView;
获得自己的父控件对象。

@property(nonatomic, readonly, copy) NSArray *subviews;
得到自己所有子控件对象

@property(nonatomic) NSInteger tag;
控件的id(标识),父控件可以通过tag来找到对应的子控件。

@property(nonatomic) CGAffineTransform transform;
控件的形变属性:可设置旋转角度,缩放比例,平移等属性。

常见方法:
-(void)addSubview:(UIView *)view;
添加1个子控件view
-(void)removeFromSuperview; 从父控件中移除
-(UIView *)viewWithTag:(NSInteger)tag;
根据1个tag标识,找到对应的控件。(一般都是子控件。)



最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 2023.5.5周五 吃了辣酱于是肚子一直很痛,睡不好,梦见到处找厕所,睡前右眼又过敏水肿,好在半夜起来去厕所发现...
    偏偏喜欢你sky阅读 119评论 0 1
  • 缘起有24种,因缘有6种,佛是能够看见世间一切缘起的人,修行必须身心合一,看见自己的缘起。 缘起诸支不应是对于生死...
    S爱笑的眼睛阅读 94评论 0 1
  • Ukrainischer Kult-Comic kommt nach Berlin:Alle lieben „Ma...
    Eva_9c90阅读 1,079评论 0 0
  • 2023.5.6 心若有所向往,何惧道阻且长。 读书卡 1.真正的修行是内观和觉察,大自然的振动频率特别高,是最接...
    坚持读写阅读 152评论 0 0
  • 希望我这个爱哭、爱笑、爱瞎想的人,调整好心态,拐个弯,与自己和解,未来的日子,会越来越好
    碎碎念丶阅读 84评论 0 0

友情链接更多精彩内容