标签:
// 添加一个事件 - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents; // 移除一个事件 - (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
/** * 1.当触摸从控件内部拖动到外部时触发:UIControlEventTouchDragExit 2.当控件之内触摸抬起时触发:UIControlEventTouchUpInside 3.当控件之外触摸抬起时触发:UIControlEventTouchUpOutside 4.触摸取消事件,设备被上锁或者电话呼叫打断:UIControlEventTouchCancel 5.用户按下时触发:UIControlEventTouchDown 6.点击计数大于1时触发:UIControlEventTouchDownRepeat 7.当触摸在控件之内拖动时触发:UIControlEventTouchDragInside 8.当触摸在控件之外拖动时触发:UIControlEventTouchDragOutside 9.当触摸从控件之外拖动到内部时触发:UIControlEventTouchDragEnter 10.当控件的值发生变化时(用于滑块、分段控件等控件):UIControlEventValueChanged 11.文本控件中开始编辑时:UIControlEventEditingDidBegin 12.文本控件中的文本被改变:UIControlEventEditingChanged 13.文本控件中编辑结束时:UIControlEventEditingDidEnd 14.文本控件内通过按下回车键结束编辑时:UIControlEventEditingDidOnExit 15.所有触摸事件:UIControlEventAllTouchEvents 16.文本编辑的所有事件:UIControlEventAllEditingEvents 17.所有事件:UIControlEventAllEvents */
#pragma mark UISwitch - (void)addSwitch { // 创建对象 // 设置frame只有origin起作用,size使用系统默认大小 UISwitch *firstSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 0, 0)]; // 开关风格的颜色 firstSwitch.tintColor = [UIColor lightGrayColor]; // 开的时候的颜色 firstSwitch.onTintColor = [UIColor greenColor]; // 按钮颜色 firstSwitch.thumbTintColor = [UIColor grayColor]; [firstSwitch setOn:YES animated:NO]; // 添加事件 [firstSwitch addTarget:self action:@selector(firstAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:firstSwitch]; } // 实现事件 - (void)firstAction:(UISwitch *)sender { if (sender.isOn) { sender.thumbTintColor = [UIColor whiteColor]; NSLog(@"开了"); }else { sender.thumbTintColor = [UIColor grayColor]; NSLog(@"关了"); } }
@interface RootView : UIView @property (nonatomic, strong) UISlider *mySlider; @property (nonatomic, strong) UIImageView *myImageView; @end
在RootView.m中实现
#import "RootView.h" @implementation RootView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // 添加子视图 [self addAllViews]; } return self; } - (void)addAllViews { // 布局slider self.mySlider = [[UISlider alloc] initWithFrame:CGRectMake(50, 50, 300, 40)]; // 滑块最小值 self.mySlider.minimumValue = 0.0; // 滑块最大值 self.mySlider.maximumValue = 2; [self addSubview:self.mySlider]; // 设置动图 self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 200, 400, 400)]; NSMutableArray *imageArray = [NSMutableArray array]; for (int i = 1; i <= 9; i++) { NSString *str = [NSString stringWithFormat:@"%d.tiff", i]; UIImage *image = [UIImage imageNamed:str]; [imageArray addObject:image]; } // 播放的动画数组 self.myImageView.animationImages = imageArray; // 播放时间 self.myImageView.animationDuration = 1; // 开始动画 [self.myImageView startAnimating]; // 添加到父视图 [self addSubview:self.myImageView]; }
在RootViewController.m中添加滑块滑动事件
#import "RootViewController.h" #import "RootView.h" @interface RootViewController () @property (nonatomic, strong) RootView *rootView; @end @implementation RootViewController - (void)loadView { self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.view = self.rootView; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // 添加滑杆滑动事件 [self.rootView.mySlider addTarget:self action:@selector(mySliderAction:) forControlEvents:UIControlEventValueChanged]; } - (void)mySliderAction:(UISlider *)sender { // 设置动图 self.rootView.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 200, 400, 400)]; NSMutableArray *imageArray = [NSMutableArray array]; for (int i = 1; i <= 9; i++) { NSString *str = [NSString stringWithFormat:@"%d.tiff", i]; UIImage *image = [UIImage imageNamed:str]; [imageArray addObject:image]; } // 播放的动画数组 self.rootView.myImageView.animationImages = imageArray; // 播放时间 self.rootView.myImageView.animationDuration = 2.1 - sender.value; // 播放次数 self.rootView.myImageView.animationRepeatCount = 0; // 开始动画 [self.rootView.myImageView startAnimating]; // 添加到父视图 [self.rootView addSubview:self.rootView.myImageView]; NSLog(@"%.2f", self.rootView.mySlider.value); }
@end
#import <UIKit/UIKit.h> @interface Root : UIView // 分段选择器 @property (nonatomic, strong) UISegmentedControl *segmentControl; // 图片视图 @property (nonatomic, strong) UIImageView *myImageView; @property (nonatomic, strong) UIImageView *myImageView1; @end
#import "Root.h" @implementation Root - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // 添加视图 [self addAllViews]; } return self; } // 添加视图 - (void)addAllViews { // 创建对象 self.segmentControl = [[UISegmentedControl alloc] initWithItems:@[@"女神", @"男神", @"程序员"]]; // 设置属性 self.segmentControl.backgroundColor = [UIColor grayColor]; self.segmentControl.frame = CGRectMake(57, 100, 300, 50); // 指定被选中的分段 self.segmentControl.selectedSegmentIndex = 0; self.segmentControl.tintColor = [UIColor colorWithRed:1.0 green:215 / 255.0 blue:0 alpha:1]; [self.segmentControl setTitle:@"aFu" forSegmentAtIndex:2]; // 添加到父视图 [self addSubview:self.segmentControl]; // 布局图片视图 self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, CGRectGetMaxY(self.segmentControl.frame) + 50, 314, 500)]; // 设置默认图片 self.myImageView.image = [UIImage imageNamed:@"女神.jpg"]; [self addSubview:self.myImageView]; } @end
#import "RootViewController.h" #import "Root.h" @interface RootViewController () @property (nonatomic, strong) Root *root; @end @implementation RootViewController - (void)loadView { self.root = [[Root alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.view = self.root; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self.root.segmentControl addTarget:self action:@selector(segmentControlAction:) forControlEvents:UIControlEventValueChanged]; } - (void)segmentControlAction:(UISegmentedControl *)sender { NSInteger index = sender.selectedSegmentIndex; switch (index) { case 0: self.root.myImageView.image = [UIImage imageNamed:@"女神.jpg"]; [self.root addSubview:self.root.myImageView]; break; case 1: self.root.myImageView.image = [UIImage imageNamed:@"男神.jpg"]; [self.root addSubview:self.root.myImageView]; break; case 2: self.root.myImageView.image = [UIImage imageNamed:@"屌丝.jpg"]; [self.root addSubview:self.root.myImageView]; break; default: break; } } @end
#import "RootViewController.h" @interface RootViewController () @property (nonatomic, strong) UIPageControl *myPage; @property (nonatomic, strong) UIView *pageView; @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self addPageControl]; // 添加事件 [self.myPage addTarget:self action:@selector(myPageAction:) forControlEvents:UIControlEventValueChanged]; } #pragma mark - UIPageControl - (void)addPageControl { self.myPage = [[UIPageControl alloc] initWithFrame:CGRectMake(100, self.view.frame.size.height - 100, self.view.frame.size.width - 200, 40)]; // 设置页数 self.myPage.numberOfPages = 4; // 设置当前页 self.myPage.currentPage = 0; self.myPage.backgroundColor = [UIColor grayColor]; //如果是单页就隐藏 self.myPage.hidesForSinglePage = YES; // 选中的圆点颜色 self.myPage.currentPageIndicatorTintColor = [UIColor whiteColor]; // 未选中的圆点颜色 self.myPage.pageIndicatorTintColor = [UIColor blackColor]; [self.view addSubview:self.myPage]; self.pageView = [[UIView alloc] initWithFrame:CGRectMake(100, self.view.frame.size.height - 450, 214, 300)]; self.pageView.backgroundColor = [UIColor grayColor]; [self.view addSubview:self.pageView]; } // 实现事件 - (void)myPageAction:(UIPageControl *)page { NSInteger index = page.currentPage; switch (index) { case 0: self.pageView.backgroundColor = [UIColor grayColor]; break; case 1: self.pageView.backgroundColor = [UIColor cyanColor]; break; case 2: self.pageView.backgroundColor = [UIColor blackColor]; break; case 3: self.pageView.backgroundColor = [UIColor brownColor]; break; default: break; } } @end
标签:
原文地址:http://www.cnblogs.com/soley/p/5398307.html