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 *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 170, 280, 300)]; imageView.backgroundColor = [UIColor brownColor]; // imageView.alpha = 0.3; [self.view addSubview:imageView]; [imageView release]; //给ImageView设置要播放的图片数组 //1.创建一个空的可变的数组 NSMutableArray *images = [NSMutableArray array]; for (int i = 0; i < 22; i++) { //2.利用for循环产生UIImage对象 NSString *name = [NSString stringWithFormat:@"Zombie%d.tiff", i]; //产生图片对象 UIImage *image = [UIImage imageNamed:name]; //添加到数组中 [images addObject:image]; } imageView.tag = 1000; //给imageView赋值 imageView.animationImages = images; //播放完全部图片的时间 imageView.animationDuration = 0.1; //重复播放的次数 imageView.animationRepeatCount = 0; //如果设置为0,则为不限次 //启动动画播放图片 // [imageView startAnimating]; [imageView 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 *imageView = (UIImageView *)[self.view viewWithTag:1000]; imageView.animationDuration = stepper.value; [imageView startAnimating]; NSLog(@"%f", stepper.value); } - (void)switchAction:(id)sender { UIImageView *imageView = (UIImageView *)[self.view viewWithTag:1000]; UISwitch *mySwitch = (UISwitch *)sender; BOOL setting = mySwitch.isOn; if (setting) { [imageView startAnimating]; NSLog(@"YES"); }else{ [imageView stopAnimating]; NSLog(@"NO"); } [leftSwitch setOn:setting animated:YES]; // [rightSwitch setOn:setting animated:YES]; } - (void)sliderAction:(UISlider *)slider { //改变僵尸的移动速度 UIImageView *imageView = (UIImageView *)[self.view viewWithTag:1000]; //利用slider的值改变imageView播放一次需要的时间 imageView.animationDuration = slider.value; [imageView 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
时间: 2024-10-26 08:17:30