iOS编程--------UIControl的作用及其子类UISegmentedControl的使用 / UISlider的使用 / UIImageView的使用

来源:互联网 发布:mastercam多轴编程教程 编辑:程序博客网 时间:2024/05/23 17:31
////  AppDelegate.h//  UI06_UIControl,controlSubClass////  Created by l on 15/9/8.//  Copyright (c) 2015年 . All rights reserved.//#import <UIKit/UIKit.h>@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@end////  AppDelegate.m//  UI06_UIControl,controlSubClass////  Created by l on 15/9/8.//  Copyright (c) 2015年 . All rights reserved.//#import "AppDelegate.h"#import "RootViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (void)dealloc{    [_window release];    [super dealloc];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];    // Override point for customization after application launch.    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];    //创建rootVC    RootViewController *rootVC = [[[RootViewController alloc] init] autorelease];    self.window.rootViewController = rootVC;  return YES;}@end///////////////////////////////////////////////////////////////////////  RootViewController.h//  UI06_UIControl,controlSubClass////  Created by l on 15/9/8.//  Copyright (c) 2015年 . All rights reserved.//#import <UIKit/UIKit.h>@interface RootViewController : UIViewController@end////  RootViewController.m//  UI06_UIControl,controlSubClass////  Created by l on 15/9/8.//  Copyright (c) 2015年 . All rights reserved.//#import "RootViewController.h"@interface RootViewController ()//分段控制@property (nonatomic, retain) UISegmentedControl *segmentedControl;//图片视图@property (nonatomic, retain) UIImageView *imageView;@end@implementation RootViewController- (void)dealloc{    [_segmentedControl release];    [_imageView release];    [super dealloc];}- (void)viewDidLoad {    [super viewDidLoad];    //一. UISegmentedControl 分段控制    //分段控制可以看做是多个button,点击不同的分段触发不同的方法    //1.创建 UISegmentedControl   _segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"男人", @"女汉子", @"伪娘", @"女人"]];    //2.设置    //常用属性    _segmentedControl.frame = CGRectMake(50, 50, 300, 50);//设置frame    _segmentedControl.tintColor = [UIColor redColor];//填充色,分段控制的边框颜色,和字体颜色    _segmentedControl.selectedSegmentIndex = 1;//设置选中分段,默认不选中    //常用方法    [_segmentedControl setTitle:@"真男人" forSegmentAtIndex:0];    //添加事件 addTarget action 继承自父类UIControl    [_segmentedControl addTarget:self action:@selector(valueChanged:) forControlEvents:(UIControlEventValueChanged)];    //3.添加    [self.view addSubview:_segmentedControl];    //4.释放    [_segmentedControl release];    //***给视图添加一个tap手势,点击一次删除一个分段    //1.创建tap    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];    //2.添加//    [self.view addGestureRecognizer:tap];    //3.释放    [tap release];    //4.实现方法    //二. 滑块控制器    //1.创建    UISlider *slider = [[UISlider alloc] initWithFrame:(CGRectMake(50, 150 , 300, 40))];    //2.设置    //常用属性    slider.backgroundColor = [UIColor cyanColor];    slider.minimumValue = 0.1;//设置最小值    slider.maximumValue = 2.5;//设置最大值    slider.value = 1;//slider当前值    slider.minimumTrackTintColor = [UIColor yellowColor];//小方向路径颜色    slider.maximumTrackTintColor = [UIColor purpleColor];//大方向路径颜色    slider.minimumValueImage = [UIImage imageNamed:@"/Users/lanou/Desktop/iOS-课堂学习/课堂-UI-Projects/UI06_UIControl,controlSubClass/UI06_UIControl,controlSubClass/sliderImage/left.jpg"];//左图片    slider.maximumValueImage = [UIImage imageNamed:@"/Users/lanou/Desktop/iOS-课堂学习/课堂-UI-Projects/UI06_UIControl,controlSubClass/UI06_UIControl,controlSubClass/sliderImage/right.jpg"];//右图片    //设置拇指按钮图片    [slider setThumbImage:[UIImage imageNamed:@"/Users/lanou/Desktop/iOS-课堂学习/课堂-UI-Projects/UI06_UIControl,controlSubClass/UI06_UIControl,controlSubClass/sliderImage/middle.jpg"] forState:(UIControlStateNormal)];    //3.添加    [self.view addSubview:slider];   //*** 添加事件    [slider addTarget:self action:@selector(sliderValueChange:) forControlEvents:(UIControlEventValueChanged)];    //4.释放    [slider release];    //创建一个imageView图片视图    _imageView = [[UIImageView alloc] initWithFrame:(CGRectMake(50, 200, 102, 102))];    //设置图片数组//    animationImages   //设置一组动态图片//    UIImage *image1 = [UIImage imageNamed:@"p1"];//    UIImage *image2 = [UIImage imageNamed:@"p2"];//    _imageView.animationImages = @[image1, image2];    NSUInteger imageCount = 10;//图片张数    NSString *imageOriginName = @"p";//图片原始名    NSString *imageSuffix = @".tiff";//图片后缀    //初始化数组    NSMutableArray *imageArray = [NSMutableArray array];    for (int i = 1; i <= imageCount; i++) {        //图片名        NSString *imageName = [NSString stringWithFormat:@"%@%d%@", imageOriginName, i, imageSuffix];        //图片        UIImage *image = [UIImage imageNamed:imageName];        //数组添加图片        [imageArray addObject:image];    }    //给imageView设置图片序列数组    _imageView.animationImages = imageArray;    //设置动画持续时间    _imageView.animationDuration = 0.5;    //设置重复次数    _imageView.animationRepeatCount = 0;//默认1次    //开始动画    [_imageView startAnimating];    //添加到view上    [self.view addSubview:_imageView];    // Do any additional setup after loading the view.}#pragma mark- tap手势- (void)tapAction:(UITapGestureRecognizer *)tap{   //1.取到分段控制控件    NSUInteger count = _segmentedControl.numberOfSegments;    //2.删除某一个分段    [_segmentedControl removeSegmentAtIndex:count - 1 animated:YES];//    [_segmentedControl removeSegmentAtIndex:0 animated:YES];}#pragma mark- 滑块方法- (void)sliderValueChange:(UISlider *)slider{     //通过slider控制另外一个控件,比如imageView的动画持续时间,动画播放进度,音频播放进度等    //取到slider的value    CGFloat value = slider.value;    //imageView动画停止    [_imageView stopAnimating];        //设置动画持续时间    _imageView.animationDuration = value;//设置播放一次一组动态图片的时间    //动画开始    [_imageView startAnimating];}#pragma mark- 分段控制触发器- (void)valueChanged:(UISegmentedControl *)segment{  //注意,方法一定要带参数,通过target action 方式,把分段控制传递过来    //取到分段控制当前选中的下标    NSUInteger index = segment.selectedSegmentIndex;    //根据选中的分段不同,触发不同的方法    switch (index) {        case 0:            NSLog(@"%@ 执行方法1", [segment titleForSegmentAtIndex:index]);            break;        case 1:            NSLog(@"%@ 执行方法2", [segment titleForSegmentAtIndex:index]);            break;        case 2:            NSLog(@"%@ 执行方法3", [segment titleForSegmentAtIndex:index]);            break;        case 3:            NSLog(@"%@ 执行方法4", [segment titleForSegmentAtIndex:index]);//            [segment removeSegmentAtIndex:index animated:YES];//越界                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
0 0
原创粉丝点击