pricechen

导航

AVPlayerViewController的使用、使用Masonry水平/垂直方向多元素布局、UIGestureRecognizer知识点总结

学习内容

欢迎关注我的iOS学习总结——每天学一点iOS:https://github.com/practiceqian/one-day-one-iOS-summary

AVPlayerVIewController使用

  1. @property (nonatomic,strong) AVPlayerViewController* playerVC;
    -------------------------------------------------------------------
    self.playerVC = [[AVPlayerViewController alloc]init];
    //设置播放源
    self.playerVC.player = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:@"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"]];
    /*
    设置视频播放的模式
      AVLayerVideoGravityResizeAspectFill等比例铺满,宽或高有可能出屏幕
      AVLayerVideoGravityResizeAspect 等比例  默认
      AVLayerVideoGravityResize 完全适应宽高
    */
    self.playerVC.videoGravity = AVLayerVideoGravityResizeAspectFill;
    //将AVPlayerViewController作为子视图控制器
    [self addChildViewController:self.playerVC];
    //设置子视图控制器位置及大小
    self.playerVC.view.frame = CGRectMake(0, 88, self.view.frame.size.width, self.view.frame.size.height/3);
    //添加到视图上
    [self.view addSubview:self.playerVC.view];
    

使用Masonry进行水平/垂直方法多元素布局

//在数组中添加四个视图元素
for (NSInteger i=0; i<4; i++) {
  HYCardViewItem* item = [[HYCardViewItem alloc]initWithFrame:CGRectMake(0, 0, 50, 77)];
  ...
  [_cardView addSubview:item];
  [carViewArr addObject:item];
}
//使用masonry的distributeViewsAlongAxis进行水平布局,这里是设置固定元素大小的,还有一种是设置固定间距的为withFixedSpacing,同样可以设置Axis参数为Vertical,可以在纵向进行布局
[carViewArr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:50.f leadSpacing:22.f tailSpacing:22.f];
[carViewArr mas_makeConstraints:^(MASConstraintMaker *make) {
  make.top.equalTo(_cardView.mas_top).offset(48);
}];

UIGestureRecognizer 手势识别总结

  • UITapGestureRecognizer点按手势

    • //创建点按手势
      UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
      //手势也可以设置代理
      tapGesture.delegate = self;
      //添加手势
      [self.imageView addGestureRecognizer:tapGesture];
      }
      //设置手势的响应方法
      - (void)tap:(UITapGestureRecognizer*)tapGesture{
      }
      -------------------------------------------------
      //代理方法,可以根据触摸的位置来决定是否接受该手势
        - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
        
      }
      
  • UITapGestureRecognizer长按手势

    • UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
      [self.imageView addGestureRecognizer:longPressGesture];
      //响应方法
      - (void)longP:(UILongPressGestureRecognizer *)longPress {
          //判断长按时的状态,根据手势状态的改变做出响应的调整
          if (longPress.state == UIGestureRecognizerStateBegan) {
          }else if (longPress.state == UIGestureRecognizerStateChanged) {
          }else if (longPress.state == UIGestureRecognizerStateEnded) {
          }
      }
      
  • UIPanGestureRecognizer平移手势/拖拽手势

    • //根据手势的偏移量,改变图片的位置
      UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
      [self.imageView addGestureRecognizer:panGes];
      
      - (void)pan:(UIPanGestureRecognizer *)pan {
        //获取偏移量
        CGPoint transP = [pan translationInView:self.imageView];
        NSLog(@"transP = %@", NSStringFromCGPoint(transP));
      
        //移动图片
        self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y);
        //清零,不要累加
        [pan setTranslation:CGPointZero inView:self.imageView];
      }
      
  • UISwipeGestureRecognizer轻扫手势

  • UIRotationGestureRecognizer旋转手势

  • UIPinchGestureRecognizer捏合手势

  1. 手势的几种代理方法

    • 以下方法均需要设置手势的代理,并遵守UIGestureRecognizerDelegate协议

      tapGesture.delegate = self
      
    • 如果一个手势A的识别部分是另一个手势B的子部分时,默认情况下A就会先识别,B就无法识别了。我们可以指定某个手势执行的前提是另一个手势失败才会识别执行,这样控制手势识别的响应顺序。

      - (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer;
      
    • 如果同一视图需要一次响应多个手势操作,可以实现下面的UIGestureRecognizerDelegate的代理方法,当返回YES的时候,可以同时响应多个手势。

      - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
      
    • 父视图如果有手势需要识别,子视图同样有相似触摸事件需要处理,这时候就可能产生冲突。

      //判断touch.view的类型
      - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
        if ([touch.view isKindOfClass:[UITableView class]]) {
          return NO;
        }
        if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
          return NO;
        }
      }
      
  2. 手势与响应者链

    • 手势与响应者链有一些差别,触摸事件首先会传递到手势上,如果手势识别成功,就会取消事件的继续传递。如果手势识别失败,事件才会被响应链处理

    • 对于 UIButton,UISwitch,UISegmentedControl,UIStepper、UIPageControl 进行单击操作,如果父视图有轻敲手势需要识别,依然会按照响应链来处理,先响应这些控件的单击事件,这仅适用于与控件的默认操作重叠的手势识别。

    • 如果子视图和父视图都有手势需要识别,则按照firstResponder从子视图到父视图传递。

posted on 2020-05-19 23:16  pricechen  阅读(594)  评论(0编辑  收藏  举报