IOS学习 UIView 页面布局

本文介绍了一个iOS应用中视图及其动画效果的实现过程,包括视图的创建、布局及通过按钮触发的动画效果调整。文章展示了如何使用UIKit框架内的UIView类创建不同颜色和位置的视图,并通过改变视图的alpha属性实现淡入淡出效果。

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

#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];

    view1.tag = 1//标示一个视图对象

//    view1 = [self.view viewWithTag:1];  //可以通过tag查找view

    view1.backgroundColor = [UIColor redColor];

    view1.clipsToBounds = YES//剪切掉视图外的内容

    [self.view addSubview:view1];

    [view1 release];

    

    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 200)];

    view2.tag = 2;

    view2.backgroundColor = [UIColor yellowColor];

    [view1 addSubview:view2];

    view2.alpha = 0.5;    //透明度

    [view2 release];

    

    UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];

    view3.tag = 2;

    view3.backgroundColor = [UIColor greenColor];

    [view1 addSubview:view3];

    [view3 release];

    

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    button.frame = CGRectMake(90,400, 140, 40);

    [button setTitle:@"change" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(changeView) forControlEvents:UIControlEventTouchUpInside];

    

    [self.view addSubview:button];

    

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, 50, 30)];

    label.text = @"jianguo";

    label.textColor = [UIColor whiteColor];

    label.font = [UIFont fontWithName:nil size:14];

    [view3 addSubview:label];

}


-(void)changeView{

    NSLog(@"print");

    

    UIView *vv = [self.view viewWithTag:2];

    [self.view addSubview:vv];

    NSLog(@"%@",vv);

    

    [UIView beginAnimations:nil context:NULL];  //需要设置代理时

    [UIView setAnimationDuration:5];     //设置动画的持续时间

    [UIView setAnimationDelay:1];     //设置动画的延迟时间

    vv.alpha = 0.0;                 //淡出

//    vv.backgroundColor = [UIColor blueColor];

    vv.alpha = 1.0;

    [UIView commitAnimations];    //动画结束

    

//    [UIView beginAnimations:nil context:NULL];

//    [UIView setAnimationDuration:10];

//    [UIView setAnimationDelay:5];

//    vv.alpha = 1.0;

//    [UIView setAnimationCurve:UIViewAnimationCurveLinear];    //动画的速率

//    vv.center = CGPointMake(300, 500);     //移动的位置

//    [UIView commitAnimations];

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值