Objective-C语言——UIButton 按钮



#import "ViewController.h"

@interface ViewController ()

{
    UIImageView *imageViewTwo;

}


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    
    //UIButton
    //凡是继承于 UIControl 的控件都具有响应事件的能力
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    
    button.frame = CGRectMake(10, 100, 100, 100);
    
    [button setTitle:@"我是按钮" forState:UIControlStateNormal];
    
    [button setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
    
    button.backgroundColor = [UIColor redColor];
    
    [button setTitle:@"高亮状态" forState:UIControlStateHighlighted];
    
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    

    //添加事件
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.view addSubview:button];
    
    
    
    
    
    
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    button2.frame = CGRectMake(130, 100, 100, 100);
    
    [button2 setTitle:@"自然状态" forState:UIControlStateNormal];
    
    [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [button2 setTitle:@"选中状态" forState: UIControlStateSelected];
    
    [button2 setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
    
    button2.backgroundColor = [UIColor lightGrayColor];
    

    //添加事件
    [button2 addTarget:self action:@selector(buttonAction2:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    [self.view addSubview:button2];
    
    
    
    
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    button3.frame = CGRectMake(250, 100, 100, 100);
    
    [button3 setTitle:@"自然状态" forState:UIControlStateNormal];
    [button3 setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
    
    [button3 setTitle:@"编辑状态" forState: UIControlStateDisabled];
    [button3 setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
    
    button3.enabled = NO;

    [self.view addSubview:button3];
    
    
    
    
    /* -------------------------------------------------- */
    
    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    aButton.frame = CGRectMake(10, 220, 100, 100);
    //自然状态
    [aButton setImage:[UIImage imageNamed:@"off.jpg"] forState:UIControlStateNormal];
    
    //高亮状态
    [aButton setImage:[UIImage imageNamed:@"on.jpg"] forState:UIControlStateHighlighted];
    
    [aButton addTarget:self action:@selector(aButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:aButton];
    
    
    
    
    UIButton *bButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    bButton.frame = CGRectMake(130, 220, 100, 100);
    
    [bButton setImage:[UIImage imageNamed:@"off.jpg"] forState:UIControlStateNormal];
    //被选中状态
    [bButton setImage:[UIImage imageNamed:@"on.jpg"] forState:UIControlStateSelected];
    
    [bButton addTarget:self action:@selector(bButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:bButton];
    
    
    
    
    
    
    
    
    //应用,按住说话
    UIButton *cButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    cButton.frame = CGRectMake(ScreenWidth/2-80/2, 400, 80, 100);
    
    [cButton setImage:[UIImage imageNamed:@"speakPhone.jpg"] forState:UIControlStateNormal];
    //按下时 响应的事件
    [cButton addTarget:self action:@selector(cButtonDown:) forControlEvents:UIControlEventTouchDown];
    
    //松开时响应的事件
    [cButton addTarget:self action:@selector(cBunttonUp:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cButton];
    
    
    UIImageView *imageViewOne = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"one.jpg"]];
    
    imageViewOne.frame = CGRectMake(ScreenWidth/2+40, 400-30, 30, 30);
    
        [self.view addSubview:imageViewOne];
    
    
    imageViewTwo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"two.jpg"]];
     imageViewTwo.frame = CGRectMake(ScreenWidth/2+40, 400-30, 30, 30);
    
    imageViewTwo.animationImages = @[[UIImage imageNamed:@"one.jpg"],[UIImage imageNamed:@"two.jpg"]];
    
    imageViewTwo.animationDuration = 0.5;
    
    imageViewTwo.hidden = YES;
    
    [self.view addSubview:imageViewTwo];
    

    
    
    
    

    
    
    
    
    
    
    
}


#pragma mark-------方法实现---------------

-(void)buttonAction:(UIButton *)sender
{

    NSLog(@"按钮响应事件");

}

-(void)buttonAction2:(UIButton *)sender
{

    sender.selected = !sender.selected;
    NSLog(@"按钮2响应事件");

}


-(void)aButtonAction:(UIButton *)sender
{

    NSLog(@"aButton响应事件");
    
}

-(void)bButtonAction:(UIButton *)sender
{
    
    sender.selected = !sender.selected;
    NSLog(@"bButton响应事件");
    
}


-(void)cButtonDown:(UIButton *)sender
{
    //显示
    imageViewTwo.hidden = NO;
    //开始动画
    [imageViewTwo startAnimating];
    NSLog(@"cButtonDown");
}

-(void)cBunttonUp:(UIButton *)sender
{
    //隐藏
    imageViewTwo.hidden = YES;
    //停止动画
    [imageViewTwo stopAnimating];
    NSLog(@"cBunttonUp");

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


好的,以下是用Objective-C纯代码实现的签到日期界面。实现的界面包括一个导航栏、一个显示当前日期的标签和一个签到按钮。 ViewController.h文件: ```objective-c #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end ``` ViewController.m文件: ```objective-c #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UILabel *dateLabel; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // 设置导航栏标题 self.navigationItem.title = @"签到"; // 设置背景颜色 self.view.backgroundColor = [UIColor whiteColor]; // 创建并添加日期标签 self.dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)]; self.dateLabel.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2 - 50); self.dateLabel.textAlignment = NSTextAlignmentCenter; self.dateLabel.textColor = [UIColor blackColor]; [self.view addSubview:self.dateLabel]; // 更新日期标签的文本 [self updateDateLabel]; // 创建并添加签到按钮 UIButton *checkInButton = [UIButton buttonWithType:UIButtonTypeSystem]; checkInButton.frame = CGRectMake(0, 0, 200, 50); checkInButton.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2 + 50); [checkInButton setTitle:@"签到" forState:UIControlStateNormal]; [checkInButton addTarget:self action:@selector(checkInButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:checkInButton]; } // 更新日期标签的文本 - (void)updateDateLabel { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy年MM月dd日"; self.dateLabel.text = [formatter stringFromDate:[NSDate date]]; } // 签到按钮点击事件 - (void)checkInButtonClicked:(UIButton *)sender { // TODO: 处理签到逻辑 // 更新日期标签的文本 [self updateDateLabel]; } @end ``` 在AppDelegate.m文件中,将ViewController设置为根视图控制器: ```objective-c #import "AppDelegate.h" #import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; ViewController *viewController = [[ViewController alloc] init]; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; self.window.rootViewController = navigationController; [self.window makeKeyAndVisible]; return YES; } ``` 这样就完成了一个简单的签到日期界面的实现。运行程序后,可以看到一个有日期标签和签到按钮的界面。点击签到按钮后,日期标签上的日期会更新为当前日期。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值