UISegmentedControl,UISlider,UISwitch,代码截屏,UIStepper的简单使用

东方建修
2023-12-01
#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

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

self.view.backgroundColor = [UIColor greenColor];

/*
//1.UISegmentedControl 是iOS中的分层控件,其实是多个button的组合视图,通过切换不同的segment(严格来说,点击不同的button),响应不同的操作
NSArray * titles = @[@"轻拍", @"长按", @"清扫", @"平移", @"捏合"];
UISegmentedControl * segmentControl = [[UISegmentedControl alloc] initWithItems:titles];
segmentControl.backgroundColor = [UIColor orangeColor];
//设置默认选中的分段
segmentControl.selectedSegmentIndex = 1;
segmentControl.frame = CGRectMake(10, 40, 300, 40);
//给segmentControl添加响应事件
[segmentControl addTarget:self action:@selector(handleSegmentControl:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentControl];
*/

UIView * aView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
aView.backgroundColor = [UIColor orangeColor];
aView.tag = 200;
[self.view addSubview:aView];
[aView release];

//2.UISlider 滑块控件,比如控制音量,以及控制视频播放进度的滑竿
//滑竿上有一系列的值,代表了一个值的范围,滑动滑竿,根据滑竿的值做出相应的改变
UISlider * slider = [[UISlider alloc] initWithFrame:CGRectMake(30, 60, 200, 30)];
//设置滑竿的最大值
slider.maximumValue = 1.0;
//设置滑竿的最小值
slider.minimumValue = 0.0;
//给slider添加响应事件
[slider addTarget:self action:@selector(handleSlider:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slider];
[slider release];

/*
//3.代码截屏
//(1.)设置要截屏的图片大小
UIGraphicsBeginImageContext(self.view.frame.size);
//(2.)对哪个视图截图固定大小的图片
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
//(3.)获取截图的图片对象
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
//(4.)结束绘制图片
UIGraphicsEndImageContext();
//(5.)保存到相册
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
*/


/*
//4.UISwitch
UISwitch * mySwitch = [[UISwitch alloc] init];
mySwitch.frame = CGRectMake(10, 100, 0, 0);
// mySwitch.backgroundColor = [UIColor blueColor];
//判断是否是开启状态(只可以得到值,不可以赋值)
// if (mySwitch.isOn) {
// NSLog(@"开");
// }else{
// NSLog(@"关");
// }
//设置开启颜色
mySwitch.onTintColor = [UIColor blackColor];
//设置关闭状态颜色
mySwitch.tintColor = [UIColor blueColor];
//设置圆形按钮颜色
mySwitch.thumbTintColor = [UIColor purpleColor];
//设置开启状态图片
// mySwitch.onImage = [UIImage imageNamed:@""];
//设置关闭状态图片
// mySwitch.offImage = [UIImage imageNamed:@""];
//设置开关状态带有动画效果(并且设置开关为打开状态)
[mySwitch setOn:YES animated:YES];
[mySwitch addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:mySwitch];
[mySwitch release];
*/

//5.UIStepper
UIStepper * myStepper = [[UIStepper alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
[self.view addSubview:myStepper];
[myStepper release];

}

- (void)switchChange:(id)sender
{
UISwitch * mySwitch = (UISwitch *)sender;
if (mySwitch.isOn) {
NSLog(@"open");
}else{
NSLog(@"close");
}
}

- (void)handleSlider:(UISlider *)slider
{
//及时获取滑块的值,改变视图的透明度
[self.view viewWithTag:200].alpha = 1 - slider.value;
}

- (void)handleSegmentControl:(UISegmentedControl *)segmentControl
{
switch (segmentControl.selectedSegmentIndex) {
case 0:
NSLog(@"轻拍");
break;
case 1:
NSLog(@"长按");
break;
case 2:
NSLog(@"清扫");
break;
case 3:
NSLog(@"平移");
break;
case 4:
NSLog(@"捏合");
break;

default:
break;
}
}

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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end
 类似资料: