MainViewController.h
#import <UIKit/UIKit.h> @interface MainViewController : UIViewController @property(nonatomic,retain)UISwitch*leftSwitch; @end
MainViewController.m
#import "MainViewController.h" @interface MainViewController () @end @implementation MainViewController @synthesize leftSwitch; - (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. //首先创建一个UIImageView UIImageView *p_w_picpathView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 170, 280, 300)]; p_w_picpathView.backgroundColor = [UIColor brownColor]; // p_w_picpathView.alpha = 0.3; [self.view addSubview:p_w_picpathView]; [p_w_picpathView release]; //给ImageView设置要播放的图片数组 //1.创建一个空的可变的数组 NSMutableArray *p_w_picpaths = [NSMutableArray array]; for (int i = 0; i < 22; i++) { //2.利用for循环产生UIImage对象 NSString *name = [NSString stringWithFormat:@"Zombie%d.tiff", i]; //产生图片对象 UIImage *p_w_picpath = [UIImage p_w_picpathNamed:name]; //添加到数组中 [p_w_picpaths addObject:p_w_picpath]; } p_w_picpathView.tag = 1000; //给p_w_picpathView赋值 p_w_picpathView.animationImages = p_w_picpaths; //播放完全部图片的时间 p_w_picpathView.animationDuration = 0.1; //重复播放的次数 p_w_picpathView.animationRepeatCount = 0; //如果设置为0,则为不限次 //启动动画播放图片 // [p_w_picpathView startAnimating]; [p_w_picpathView stopAnimating]; //UISegmentedControl UISegmentedControl *segmeng = [[UISegmentedControl alloc] initWithItems:@[@"僵尸",@"丧尸",@"吸血鬼"]]; segmeng.frame = CGRectMake(40, 35, 240, 30); segmeng.backgroundColor = [UIColor magentaColor]; segmeng.alpha = 0.3; segmeng.layer.cornerRadius = 7; segmeng.tintColor = [UIColor yellowColor];//改变颜色 // 绑定一个触发事件(方法) [segmeng addTarget:self action:@selector(segmengAction:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:segmeng]; [segmeng release]; //UISlider的使用 UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(40, 70, 240, 30)]; [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged]; //改变slider的最大值 slider.maximumValue = 10; //最小值 slider.minimumValue = 0.1; //slider.maximumValueImage [self.view addSubview:slider]; [slider release]; //UISwitch的使用 leftSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(40, 100, 30, 20)]; [leftSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:leftSwitch]; //UIStepper 的使用 UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(160 , 100, 40, 20)]; stepper.backgroundColor = [UIColor blueColor]; stepper.alpha = 0.3; // Set action target and action for a particular value changed event [stepper addTarget:self action:@selector(stepper:) forControlEvents:UIControlEventValueChanged]; // Set min and max [stepper setMinimumValue:0.1]; [stepper setMaximumValue:10]; [stepper setWraps:YES]; //if YES, value wraps from min <-> max. default = NO // [stepper setContinuous:NO]; [stepper setStepValue:0.5];//设置每次跳动的值 [self.view addSubview:stepper]; } - (void)stepper:(UIStepper *)stepper { UIImageView *p_w_picpathView = (UIImageView *)[self.view viewWithTag:1000]; p_w_picpathView.animationDuration = stepper.value; [p_w_picpathView startAnimating]; NSLog(@"%f", stepper.value); } - (void)switchAction:(id)sender { UIImageView *p_w_picpathView = (UIImageView *)[self.view viewWithTag:1000]; UISwitch *mySwitch = (UISwitch *)sender; BOOL setting = mySwitch.isOn; if (setting) { [p_w_picpathView startAnimating]; NSLog(@"YES"); }else{ [p_w_picpathView stopAnimating]; NSLog(@"NO"); } [leftSwitch setOn:setting animated:YES]; // [rightSwitch setOn:setting animated:YES]; } - (void)sliderAction:(UISlider *)slider { //改变僵尸的移动速度 UIImageView *p_w_picpathView = (UIImageView *)[self.view viewWithTag:1000]; //利用slider的值改变p_w_picpathView播放一次需要的时间 p_w_picpathView.animationDuration = slider.value; [p_w_picpathView startAnimating]; //在重新播放 NSLog(@"%f",slider.value); } - (void)segmengAction:(UISegmentedControl *)seg { NSLog(@"%d",seg.selectedSegmentIndex); if (0 == seg.selectedSegmentIndex) { NSLog(@"僵尸"); }else if (1 == seg.selectedSegmentIndex){ NSLog(@"丧尸"); }else if(2 == seg.selectedSegmentIndex){ NSLog(@"吸血鬼"); } } - (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
转载于:https://blog.51cto.com/liuyafang/1548577