UI控件(3)

文章目录

    • 1、 UIPageViewController
      • 1.1 介绍
      • 1.2 语法
    • 2、UIActivityIndicatorView
      • 2.1 介绍
      • 2.2 语法
    • 3、UIGestureRecognizer
      • 3.1 介绍
      • 3.2 语法
        • Tap
          • 1、介绍
          • 2、常用方法
        • Pan
          • 1、介绍
          • 2、常用方法
          • 3、注意点
        • LongPress
          • 1、介绍
          • 2、常用方法
        • Swipe
          • 1、介绍
          • 2、常用方法

1、 UIPageViewController

1.1 介绍

UIPageViewController是iOS 5.0之后提供的一个分页控件可以实现图片轮播效果和翻书效果.使用起来也很简单方便.

1.2 语法

返回上一个ViewController对象

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    
    NSUInteger index = [self indexOfViewController:(ContentViewController *)viewController];
    if ((index == 0) || (index == NSNotFound)) {
        return nil;
    }
    index--;
    // 返回的ViewController,将被添加到相应的UIPageViewController对象上。
    // UIPageViewController对象会根据UIPageViewControllerDataSource协议方法,自动来维护次序
    // 不用我们去操心每个ViewController的顺序问题
    return [self viewControllerAtIndex:index];
}

返回下一个ViewController对象

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    
    NSUInteger index = [self indexOfViewController:(ContentViewController *)viewController];
    if (index == NSNotFound) {
        return nil;
    }
    index++;
    if (index == [self.pageContentArray count]) {
        return nil;
    }
    return [self viewControllerAtIndex:index];
  
}

根据index得到对应的UIViewController

- (ContentViewController *)viewControllerAtIndex:(NSUInteger)index {
    if (([self.pageContentArray count] == 0) || (index >= [self.pageContentArray count])) {
        return nil;
    }
    // 创建一个新的控制器类,并且分配给相应的数据
    ContentViewController *contentVC = [[ContentViewController alloc] init];
    contentVC.content = [self.pageContentArray objectAtIndex:index];
    return contentVC;
}

2、UIActivityIndicatorView

2.1 介绍

活动指示器,作用是指示网络数据获取的状态或者App处理某件事的状态。为了消除用户的急躁心里。UIActivityIndicatorView 是继承与 UIView。UIView有的方法UIActivityIndicatorView 也可以使用,俗称“菊花”。

2.2 语法

UIActivityIndicatorView *testActivityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
testActivityIndicator.center = CGPointMake(100.0f, 100.0f);//只能设置中心,不能设置大小
testActivityIndicator.color = [UIColor redColor]; // 改变圈圈的颜色为红色; iOS5引入
[testActivityIndicator startAnimating]; // 开始旋转
[self.view addSubview:testActivityIndicator];
// [testActivityIndicator stopAnimating]; // 结束旋转

initWithActivityIndicatorStyle是UIActivityIndicatorView唯一的初始化方法
属性值是一个枚举变量,只有三个值:
UIActivityIndicatorViewStyleWhite; 白色圆圈
UIActivityIndicatorViewStyleWhiteLarge; 白色圆圈 但是要大些
UIActivityIndicatorViewStyleGray; 灰色圆圈

3、UIGestureRecognizer

3.1 介绍

手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加了移动设备使用便捷性。手势识别UIGestureRecognizer类是个抽象类,下面的子类是具体的手势,开发这可以直接使用这些手势识别。一个手势只能对应一个View,但是一个View可以有多个手势。

  • UITapGestureRecognizer
  • UIPinchGestureRecognizer
  • UIRotationGestureRecognizer
  • UISwipeGestureRecognizer
  • UIPanGestureRecognizer
  • UILongPressGestureRecognizer

上面的手势对应的操作是:

  • Tap(点一下)
  • Pinch(二指往內或往外拨动,平时经常用到的缩放)
  • Rotation(旋转)
  • Swipe(滑动,快速移动)
  • Pan (拖移,慢速移动)
  • LongPress(长按)

3.2 语法

Tap

1、介绍

点击手势

2、常用方法
// 点击手势
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[_imageView addGestureRecognizer:tapGestureRecognizer];

Pan

1、介绍

拖动手势

2、常用方法
// 要求的最小触摸数量,默认是1
@property (nonatomic) NSUInteger minimumNumberOfTouches __TVOS_PROHIBITED;

// 允许的最大触摸数量,默认是无穷大
@property (nonatomic) NSUInteger maximumNumberOfTouches __TVOS_PROHIBITED;

// 位移
- (CGPoint)translationInView:(nullable UIView *)view;

// 设置位移
- (void)setTranslation:(CGPoint)translation inView:(nullable UIView *)view;

// 位移速度
- (CGPoint)velocityInView:(nullable UIView *)view;
3、注意点

locationView是UIGestureRecognizer的属性
translationInView是UIPanGestureRecognizer的属性

区别:
locationView的话,最好是在控制器里面使用,然后传值设置为控制器的view。得出一个新的点,这个点就是移动后的点。移动控件的中心点的话,直接给他赋值这个新的point就好了。translationInView的话,设置传值为控件本身,得到的点,是指手指在控件上移动的offset。所以给控件的中心点加上这个offset,就可以使他准确的移动。

- (void)translationInViewTest:(UIPanGestureRecognizer *)panGesture{
    CGPoint currentP = [panGesture translationInView:self];
    self.center = CGPointMake(self.center.x + currentP.x, self.center.y + currentP.y);
        [panGesture setTranslation:CGPointZero inView:self];
}
- (void)locationInView:(UIPanGestureRecognizer *)panGesture{
    CGPoint currentLIV = [panGesture locationInView:self.superview];
    NSLog(@"currentTPPp%@",NSStringFromCGPoint(currentLIV));
    self.center = currentLIV;
    [self translationInViewTest:panGesture];
}

LongPress

1、介绍

使用长按手势时用户必须使用一个或多个手指,按压视图一定时间才能触发响应。

2、常用方法

UILongPressGestureRecognizer类包含以下四个属性:

  • minimumPressDuration:触发长按手势所需按压的最短时间,单位是秒,默认为0.5秒。
  • numberOfTouchesRequired:触发长按手势所需手指数,默认为1。
  • numberOfTapsRequired:触发长按手势所需点击数,默认为0。
  • allowableMovement:手指按压住视图后允许手指移动的最大距离,单位point,默认10 points

Swipe

1、介绍

滑动手势可以滑向上下左右的任一方向,但不包括对角线方向,使用UISwipeGestureRecognizerDirection类提供的dircetion属性指定手势的滑动方向,如果没有设置滑动方向,默认向右侧滑动。每一个手势识别器只能识别一个滑动方向,所以如果需要识别多个方向,需要添加多个手势识别器。另外,滑动手势识别器的动作方法在滑动手势结束那一刻被调用。滑动手势可快可慢。慢的滑动需要高精度方向,但所需动距离短些;快速滑动需要低精度的方向,但所需滑动距离长些。

2、常用方法
// viewRed添加左滑手势识别器
UISwipeGestureRecognizer *swipeLeftRed = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(slideToLeftWithGestureRecognizer:)];
swipeLeftRed.direction = UISwipeGestureRecognizerDirectionLeft;
[self.viewRed addGestureRecognizer:swipeLeftRed];

// viewBlack添加右滑手势识别器
UISwipeGestureRecognizer *swipeRightBlack = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(slideToRightWithGestureRecognizer:)];
swipeRightBlack.direction = UISwipeGestureRecognizerDirectionRight;
[self.viewBlack addGestureRecognizer:swipeRightBlack];

你可能感兴趣的:(UI控件(3))