iOS -UI-视图创建及动画的实现

本文介绍了一个 iOS 应用的 UI 动画实现案例,包括按钮点击反馈、图片移动和透明度变化等效果。通过定时器触发动画,演示了不同组件在交互中的视觉表现。

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

//

//  ViewController.m

//  UI-视图练习

//

//  Created by jzq_mac on 15/7/23.

//  Copyright (c) 2015 jzq_mac. All rights reserved.

//


#import "ViewController.h"


@interface ViewController ()

{

    UIButton *button;

    UILabel *label;

    UITextField *textFiled;


}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.view.backgroundColor = [UIColor blackColor];

    

    [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(moveAction) userInfo:nil repeats:YES];

    

//    UIButton 练习

    

    button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(150, 567, 100, 100);

    [self.view addSubview:button];

    button.layer.cornerRadius = 50;

    button.backgroundColor = [UIColor blueColor];

//    [button setTitle:@"点我啊" forState:UIControlStateNormal];

    [button setTitle:@"点我干嘛" forState:UIControlStateSelected];

    button.showsTouchWhenHighlighted = YES;

    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    button.titleLabel.font = [UIFont systemFontOfSize:20];

    button.clipsToBounds = YES;

    button.contentMode = YES;

    button.alpha = 0.8;

    [button setTitleShadowColor:[UIColor yellowColor] forState:UIControlStateNormal];

    [button setBackgroundImage:[UIImage imageNamed:@"苹果"] forState:UIControlStateNormal];

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

    

   



//    UIlabel 练习

    

    label = [[UILabel alloc]initWithFrame:CGRectMake(170, 70, 50, 500)];

    [self.view addSubview:label];

    label.backgroundColor = [UIColor redColor];

    label.text = @"梦想还是要有的 万一实现了呢";

    label.layer.cornerRadius = 50;

    label.layer.masksToBounds = YES;

    label.textColor = [UIColor yellowColor];

    label.textAlignment = NSTextAlignmentCenter;

    label.font = [UIFont systemFontOfSize:30];

    label.numberOfLines = 50;


    for (int i = 0; i < 2; i ++) {

        UIImageView *imgeView = [[UIImageView alloc]initWithFrame:CGRectMake(20 + (240 + 20)*i, 587, 80, 80)];

        [self.view addSubview:imgeView];

        imgeView.backgroundColor = [UIColor redColor];

        imgeView.layer.cornerRadius = 40;

        imgeView.layer.masksToBounds = YES;

        imgeView.image = [UIImage imageNamed:@"苹果1"];

        imgeView.tag = i + 100;

        }



    

    

    textFiled = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 375, 50)];

    textFiled.delegate = self;

    [self.view addSubview:textFiled];

    textFiled.backgroundColor = [UIColor grayColor];

    textFiled.placeholder = @"欢迎来到星际穿越";

    textFiled.textAlignment = NSTextAlignmentCenter;

    textFiled.font = [UIFont systemFontOfSize:30];

    textFiled.returnKeyType = UIReturnKeyDefault;

    UIImageView *left = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];

    left.image = [UIImage imageNamed:@"拳头"];

    textFiled.leftView = left;

    textFiled.leftViewMode = UITextFieldViewModeAlways;

    textFiled.alpha = 0.5;

    

    

}


- (void)textFieldDidBeginEditing:(UITextField *)textField

{

    

}


- (void)textFieldDidEndEditing:(UITextField *)textField

{

    

}


- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    

    [textFiled resignFirstResponder];

   

    return YES;

}


- (void)moveAction

{

    //  图片移动练习

    [UIView animateWithDuration:2 animations:^{button.frame = CGRectMake(150, 0, 100, 100);} completion:^(BOOL finished){[UIView animateWithDuration:2 animations:^{button.frame = CGRectMake(150, 567, 100, 100);}];}];

    

    

    

    for (int i =0; i<2; i++) {

        

        UIImageView *imgeView = (UIImageView *)[self.view viewWithTag:i+100];

        

        if (imgeView.tag == 100) {

            

            imgeView.alpha = 1;

            [UIView animateWithDuration:2 animations:^{

                imgeView.alpha = 0.0;

            }];


            [UIView animateWithDuration:2 animations:^{

                imgeView.frame = CGRectMake(160, 10, 80, 80);

            } completion:^(BOOL finished) {

                [UIView animateWithDuration:2 animations:^{

                    imgeView.frame = CGRectMake(20, 587, 80, 80);

                }];

            }];

        }else{

            

            imgeView.alpha = 1;

            [UIView animateWithDuration:2 animations:^{

                imgeView.alpha = 0.0;

            }];

            [UIView animateWithDuration:2 animations:^{

                imgeView.frame = CGRectMake(160, 10, 80, 80);

            } completion:^(BOOL finished) {

                [UIView animateWithDuration:2 animations:^{

                    imgeView.frame = CGRectMake(270, 587, 80, 80);

                }];

            }];

        }

        

        

        

        

    }

    

    

    label.alpha = 1;

    [UIView animateWithDuration:4 animations:^{

        label.alpha = 0.0;

    }];

    label.text = @"梦想还是要有的 万一实现了呢";

    [UIView animateWithDuration:3 animations:^{

        label.text = @"梦想还是要有的 万一实现了呢";

    } completion:^(BOOL finished) {

        [UIView animateWithDuration:1 animations:^{

            label.text = @"我就是我颜色不一样的花火";

        }];

    }];

    

    

    

}



- (void)click:(UIButton *)sender

{

    if (sender.selected != YES) {

        [button setBackgroundImage:[UIImage imageNamed:@"星际"] forState:UIControlStateNormal];

        [button setTitle:@"点我啊" forState:UIControlStateNormal];

        sender.selected = YES;

    }else{

        [button setBackgroundImage:[UIImage imageNamed:@"拳头"] forState:UIControlStateNormal ];

        [button setTitle:@"点我干嘛" forState:UIControlStateSelected];

        sender.selected = NO;

    }

    

}


- (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、付费专栏及课程。

余额充值