Target Action And UIControl
target-action的设计模式
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(135, 100, 80, 40); [button setTitle:@"??" forState:UIControlStateNormal]; button.backgroundColor = [UIColor colorWithRed:0.510 green:0.983 blue:1.000 alpha:1.000]; button.titleLabel.font = [UIFont systemFontOfSize:30]; [button setTintColor:[UIColor whiteColor]]; // button.showsTouchWhenHighlighted = YES; [button addTarget:self action:@selector(press:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; - (void)press:(UIButton *)button { button.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255. green:arc4random() % 256 / 255. blue:arc4random() % 256 / 255. alpha:1.000]; }
target-action的设计模式(目标动作机制)
多用于view的设计, 用于降低耦合性
UIControl, 继承于UIView, 内部封装了tar get-action的设计模式
TouchViewProPro *touchViewProPro = [[TouchViewProPro alloc] initWithFrame:CGRectMake(130, 320, 100, 100)]; touchViewProPro.backgroundColor = [UIColor orangeColor]; [touchViewProPro addTarget:self action:@selector(change:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:touchViewProPro]; [touchViewProPro release]; - (void)change:(TouchViewProPro *)view { // NSLog(@"change"); 打印当前方法的名字 NSLog(@"%s", __FUNCTION__); 打印行数 NSLog(@"%d", __LINE__); 移除某个方法 [view removeTarget:self action:@selector(change:) forControlEvents:UIControlEventTouchUpInside]; }
UIControl的子类
1.UIButton, 按钮
2.UITextField, 单行文本输入框
3.UISegmentedControl, 分段控制器
4.UISlider, 滑块
5.UIStepper, 计步器
创建一个BUtton UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(140, 80, 80, 40); button.backgroundColor = [UIColor blueColor]; [button setTitle:@"??" forState:UIControlStateNormal]; [button addTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; 创建TextField textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 160, 375 - 200, 40)]; textField.borderStyle = UITextBorderStyleRoundedRect; textField.placeholder = @" TextField"; textField.text = @"吃饭"; [self.view addSubview:textField]; [textField release]; UISegmentedControl, 分段控制器, 继承于UIControl NSArray *array = [NSArray arrayWithObjects:@"吃饭", @"睡觉", @"打豆豆", nil]; UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:array]; segmentedControl.frame = CGRectMake(100, 220, 375 - 200, 40); segmentedControl.backgroundColor = [UIColor whiteColor]; 属性 默认选中哪个分段 注: 设置选中某个分段, 只修改了样式, 没有触发关联的方法 segmentedControl.selectedSegmentIndex = 0; 渲染色, 默认是蓝色 segmentedControl.tintColor = [UIColor colorWithRed:0.000 green:0.502 blue:1.000 alpha:1.000]; [segmentedControl addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged]; segmentedControl.tag = 100; [self.view addSubview:segmentedControl]; [segmentedControl release]; UISlider, 滑块, 继承于UIControl UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(100, 280, 375 - 200, 40)]; 最大值, 默认: 1.0 slider.maximumValue = 100; 最小值, 默认: 0.0 slider.minimumValue = 50; 当前值, 默认为最小值 slider.value = 62.5; 划过去的轨道颜色, 默认蓝色 slider.minimumTrackTintColor = [UIColor redColor]; 未划过去的轨道颜色, 默认浅灰色 slider.maximumTrackTintColor = [UIColor greenColor]; 设置滑块的颜色 [slider setThumbTintColor:[UIColor blueColor]]; 关联方法 [slider addTarget:self action:@selector(slide:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:slider]; [slider release]; UIStepper, 计步器, 继承于UIControl UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(140, 340, 375 - 200, 40)]; 最小值, 默认0.0 stepper.minimumValue = 1; 最大值, 默认100 stepper.maximumValue = 10; 当前值 stepper.value = 5; 改变量, 默认1 stepper.stepValue = 2; 渲染色 stepper.tintColor = [UIColor colorWithRed:1.000 green:0.521 blue:0.342 alpha:1.000]; 关联方法 [stepper addTarget:self action:@selector(step:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:stepper]; [stepper release]; } - (void)step:(UIStepper *)stepper { NSLog(@"%s", __FUNCTION__); NSLog(@"value = %.2f", stepper.value); } - (void)slide:(UISlider *)slider { NSLog(@"%s", __FUNCTION__); NSLog(@"%.2lf", slider.value); } - (void)change:(UISegmentedControl *)segmentedControl { NSLog(@"%s", __FUNCTION__); NSLog(@"%ld", segmentedControl.selectedSegmentIndex); //获取分段控制器的某个标题 NSString *title = [segmentedControl titleForSegmentAtIndex:segmentedControl.selectedSegmentIndex]; NSLog(@"%@", title); textField.text = title; } - (void)pressButton { NSLog(@"%s", __FUNCTION__); UISegmentedControl *control = (UISegmentedControl *)[self.view viewWithTag:100]; 分段控制的分段数 NSLog(@"%lu", control.numberOfSegments); 插入一个分段 [control insertSegmentWithTitle:@"看电影" atIndex:control.numberOfSegments animated:YES]; }
UIImage, 图片类, 继承于NSObject
UIImageView, 继承于UIView, 用于显示图片
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]]; imageView.frame = CGRectMake(0, 0, 500 , 360); imageView.center = self.view.center; [self.view addSubview:imageView]; [imageView release]; 播放多张图片 NSMutableArray *marray = [[NSMutableArray alloc] initWithCapacity:14]; for (NSInteger i = 1; i <= 14; i++) { NSString *name = [NSString stringWithFormat:@"%ld", i]; UIImage *image = [UIImage imageNamed:name]; [marray addObject:image]; } 动画数组, 数组内存放UIImage imageView.animationImages = marray; 动画时长 imageView.animationDuration = 1; 动画重复次数 imageView.animationRepeatCount = 10; //开始动画 [imageView startAnimating]; _flag = YES; // // [imageView stopAnimating]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(135, 20, 100, 40); [button setTitle:@"停止" forState:UIControlStateNormal]; [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; button.titleLabel.font = [UIFont systemFontOfSize:20]; [button addTarget:self action:@selector(stop:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } - (void)stop:(UIButton *)button { NSLog(@"%s", __FUNCTION__); // if (_flag) { // [imageView stopAnimating]; // _flag = NO; // } else { // [imageView startAnimating]; // _flag = YES; // } if (imageView.isAnimating) { [imageView stopAnimating]; [button setTitle:@"开始" forState:UIControlStateNormal]; } else { [imageView startAnimating]; [button setTitle:@"停止" forState:UIControlStateNormal]; } }
2015-10-22 19:51:07
时间: 2024-12-23 01:13:32