欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

UIButton与定时器

程序员文章站 2022-03-29 17:54:16
目录UIButton1. 基础知识2. UIButton事件定时器UIButton1. 基础知识通过类方法创建:类名+方法名ViewController.h 中@interface ViewController ()@end@implementation ViewController//创建普通按钮- (void) createUIRectButton { //创建一个btn对象,根据类型来创建btn //圆角类型btn:UIButtonTypeRoundedRe...

UIButton

1. 基础知识

  • 通过类方法创建:类名+方法名
  • ViewController.h 中
@interface ViewController ()

@end

@implementation ViewController

//创建普通按钮
- (void) createUIRectButton {
    //创建一个btn对象,根据类型来创建btn
    //圆角类型btn:UIButtonTypeRoundedRect
    //通过类方法创建:类名+方法名
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(100, 100, 100, 30);
    //设置按钮的文字内容
	//P1: 字符串类型,显示到按钮上的文字
	//P2: 设置文字类型的状态类型:UIControlStateNormal正常状态
    [btn setTitle:@"按钮1" forState:UIControlStateNormal];
		
	//P1: 显示的文字
	//P2: 显示文字的状态类型:UIControlStateHighlighted按下状态
    [btn setTitle:@"按下状态" forState:UIControlStateHighlighted];
	//背景颜色
	[btn.backgroundColor = [UIColor grayColor];
    //设置按钮风格颜色
    [btn setTintColor:[UIColor whiteColor]];//不分状态,优先级低于setTitleColor

    //设置文字显示的颜色
		[btn setTitleColor: [UIColor redColor]forState:UIControlStateNormal];
	//设置按下状态的颜色
		[btn setTitleColor:[UIColor yellowColor]
		forState:UIControlStateHighlighted];
	//修改字体大小
		btn.titleLabel.font = [UIFont systemFontOfSize:13];
	//添加到视图中并显示
		[self.view addSubview:btn];
}
//创建一个可以显示图片的按钮
- (void) creatImageBtn {
	//自定义类型的btn
    UIButton *btnImage = [UIButton buttonWithType:UIButtonTypeCustom];
    
    btnImage.frame = CGRectMake(100, 200, 100, 100);
    UIImage *icon01 = [UIImage imageNamed:@"btn01.jpg"];
    UIImage *icon02 = [UIImage imageNamed:@"btn02.jpg"];
    
	//设置按钮图片方法设置
	//p1:显示图片的对象
	//p2:控件的状态
    [btnImage setImage:icon01 forState:UIControlStateNormal];
    [btnImage setImage:icon02 forState:UIControlStateHighlighted];
	[self.view addSubview:btnImage];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createUIRectButton];
		[self creatImageBtn];
}

2. UIButton事件

@interface ViewController ()

@end

@implementation ViewController//视图控制器是self
//创建按钮函数
- (void) createBtn {
		//创建圆角按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    btn.frame = CGRectMake(100, 100, 100, 40);
    
    [btn setTitle:@"按钮" forState:UIControlStateNormal];
    //向按钮添加事件函数
    [btn addTarget:self action: @selector(pressBtn) 
				forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btn];
		UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    btn1.frame = CGRectMake(100, 200, 80, 40);
    [btn1 setTitle:@"按钮1" forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    btn.tag = 101;
    btn1.tag = 102;
    [self.view addSubview:btn1];
    [self.view addSubview:btn];
}

- (void)pressBtn: (UIButton *) btn{
    if(btn.tag == 101){
        NSLog(@"press1 the button");
    }
    if(btn.tag == 102){
        NSLog(@"press2 the button");
    }
}

- (void)pressBtn{
    NSLog(@"press the button");
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createBtn];
}

[btn addTarget:self action: @selector(pressBtn) forControlEvents:UIControlEventTouchUpInside];中:

  • P1:“谁”来实现事件函数,实现的对象就是“谁”
  • P2:@selector(pressBtn):函数对象,当按钮满足P3时间类型时,调用函数
  • P3:UIControlEvent:事件处理函数类型
  • UIControlEventTouchUpInside:当手指离开屏幕时并且手指在按钮范围内触发事件函数
  • UIControlEventTouchDown:当手指触碰到屏幕上时

定时器

  • 定时器对象:可以在固定时间发送一个消息,通过此消息来调用相应的事件函数,通过此函数可以在固定事件段完成一个根据时间间隔的任务

  • SceneDelegate.h文件中

@interface SceneDelegate : UIResponder <UIWindowSceneDelegate>
@property (strong, nonatomic) UIWindow * window;
@end
  • ViewController.h文件中
@interface ViewController : UIViewController {
    NSTimer *_timerView;
}
//定义属性
@property(retain, nonatomic) NSTimer* timerView;
@end
  • ViewController.m文件中
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

//属性和成员变量的同步
@synthesize timerView = _timerView;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(100, 100, 40, 100);
    [btn setTitle:@"active" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(startPress) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    UIButton *btnStop = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnStop.frame = CGRectMake(100, 200, 40, 100);
    [btnStop setTitle:@"stop" forState:UIControlStateNormal];
    [btnStop addTarget:self action:@selector(stopPress) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btnStop];
    UIView * view = [[UIView alloc] init];
    view.frame = CGRectMake(0, 0, 80, 80);
    view.backgroundColor = [UIColor redColor];

    [self.view addSubview:view];
    view.tag = 101;//用tag来访问
}
//按下定时器按钮函数
-(void) startPress{
//NStimer类方法创建一个定时器并且启动这个定时器
//返回值为一个新建好的定时器对象
    _timerView = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateTimer:) userInfo:@"rose" repeats:YES];
}
//定时器函数
//可以将定时器本身作为参数传入
-(void) updateTimer: (NSTimer *) timer{
    UIView* view = [self.view viewWithTag:101];//访问view,tag从100开始
    view.frame = CGRectMake(view.frame.origin.x+1, view.frame.origin.y+1, 80, 80) ;
    NSLog(@"test %@ ", timer.userInfo);
}

//停止按钮函数
-(void) stopPress {
    [_timerView invalidate];
}
@end

_timerView = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateTimer:) userInfo:@"rose" repeats:YES];中:

  • P1:每隔多长时间调用定时器函数,以秒为单位
  • P2:表示实现定时器函数的对象
  • P3: 定时器函数对象
  • P4: 可以定时器函数中的一个参数,无参数传nil
  • P5: 定时器是否重复操作YES重复/NO只完成一次

通过调整_timerView = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateTimer:) userInfo:@"rose" repeats:YES];方法中的参数来调整返回事件的时间间隔。其中-(void) updateTimer: (NSTimer *) timer函数可以通过调整每次调用该函数时小方块的位置来控制小方块的运动形态和运动轨迹,时间间隔越短,运动位置越少,小方块运动的就越平滑

本文地址:https://blog.csdn.net/weixin_45747214/article/details/107454087

相关标签: ios objective-c