iOS自学笔记之UIStepper、UISegmentedControl和UITouch

/

软硬件环境

  • macOS Sierra
  • Xcode 7.0.1

UIStepper

UIStepper是步进器控件,根据设定的步长进行调整

  1. //创建一个UIStepper对象并设置其位置
  2. UIStepper* stepper = [[UIStepper alloc] initWithFrame:CGRectMake(100, 100, 80, 40)] ;
  3. //最小值
  4. stepper.minimumValue = 0 ;
  5. //最大值
  6. stepper.maximumValue = 100 ;
  7. //步进的幅度
  8. stepper.stepValue = 5 ;
  9. //持续响应事件
  10. stepper.autorepeat = YES ;
  11. //是否将步进结果通过事件响应函数反应出来
  12. stepper.continuous = NO ;
  13. //添加事件
  14. [stepper addTarget:self action:@selector(stepperPress:) forControlEvents:UIControlEventValueChanged] ;
  15. [self.view addSubview:stepper] ;

响应事件函数实现

  1. //stepper响应事件
  2. - (void)stepperPress:(UIStepper*)stepper {
  3. NSLog(@"stepper value:%f",stepper.value) ;
  4. }

代码运行结果

uiswitch_01

UISegmentedControl

UISegmentedControl是iOS中的分栏控件,它类似于一个选择器,所有元素当中只可以选择一个。

  1. //创建一个UISegmentedControl对象并设置其位置
  2. UISegmentedControl* segControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(10, 300, 300, 40)] ;
  3. //添加3个元素
  4. [segControl insertSegmentWithTitle:@"简单" atIndex:0 animated:YES] ;
  5. [segControl insertSegmentWithTitle:@"一般" atIndex:1 animated:YES] ;
  6. [segControl insertSegmentWithTitle:@"困难" atIndex:2 animated:YES] ;
  7. //初始选中元素
  8. segControl.selectedSegmentIndex = 0 ;
  9. //添加事件
  10. [segControl addTarget:self action:@selector(segControlPress:) forControlEvents:UIControlEventValueChanged] ;
  11. [self.view addSubview:segControl] ;

事件响应函数

  1. //UISegmentedControl事件响应函数
  2. - (void)segControlPress:(UISegmentedControl*)segControl {
  3. NSLog(@"segControl %ld",segControl.selectedSegmentIndex) ;
  4. }

代码执行结果

uiswitch_01

UITouch

  1. //当手指触碰到屏幕时,执行该方法
  2. - (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  3. NSLog(@"touchesBegan") ;
  4. UITouch* touch = [touches anyObject] ;
  5. if (touch.tapCount == 1) {
  6. NSLog(@"单击!") ;
  7. } else if (touch.tapCount == 2) {
  8. NSLog(@"双击!") ;
  9. }
  10. }
  11. //当手指在屏幕上滑动时,执行该方法
  12. - (void) touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  13. NSLog(@"touchesMoved") ;
  14. }
  15. //当手指离开屏幕时,执行该方法
  16. - (void) touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  17. NSLog(@"touchesEnded") ;
  18. }
  19. //当手指操作被其它事件中断时,执行该方法,如常见的来电中断
  20. - (void) touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  21. NSLog(@"touchesCancelled") ;
  22. }

uiswitch_01

源码下载

https://github.com/djstava/iOSLearning/tree/master/iOSUIStepperAndUISegmentedControl

转载请注明作者和出处,并添加本页链接。
原文链接:http://xugaoxiang.com/post/45

给我留言