IOS开发中用开关(UISwitch)跟滑块(UISlider)控制手机屏幕的亮度
.h文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property(strong,nonatomic)UISlider *mysld;
@property(strong,nonatomic)UISwitch *myswitch;
@property(strong,nonatomic)UIImageView *imageview;
@end
.m文件
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.imageview=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"phone.jpg" ]];
self.imageview.frame=self.view.bounds;
self.imageview.autoresizingMask=UIViewAutoresizingFlexibleWidth;
[self.view insertSubview:self.imageview atIndex:0];
//创建一个滑块
self.mysld=[[UISlider alloc]initWithFrame:CGRectMake(100, 200, 250, 50)];
[self.view addSubview:self.mysld];
//创建一个开关
self.myswitch=[[UISwitch alloc]initWithFrame:CGRectMake(150, 100, 150, 150)];
[self.view addSubview:self.myswitch];
[self.myswitch addTarget:self action:@selector(changevalue) forControlEvents:UIControlEventValueChanged];
[self.mysld addTarget:self action:@selector(textvalue) forControlEvents:UIControlEventValueChanged];
}
-(void)changevalue
{
if (self.myswitch.isOn) {
//设置屏幕默认亮度为0.5
[self.mysld setValue:0.5];
self.imageview.alpha=self.mysld.value;
} else
{
[self.mysld setValue:0.0];
self.imageview.alpha=self.mysld.value;
}
}
-(void)textvalue
{
if (self.myswitch.isOn) {
self.imageview.alpha=self.mysld.value;
}
}