在UIPanGesture识别期间设置要查看的帧

时间:2016-10-11 07:40:12

标签: ios objective-c uipangesturerecognizer

我正在UIPanGesture识别UIView(中心灰色)时尝试更新两个标签的帧。

功能

我需要选择英语和阿拉伯语之间的语言。中间有一个滑块按钮(灰色视图),我已经应用了UIPanGesture。因此,当向阿拉伯语移动时,语言英语应移至中心并假定其已选中,反之亦然。

我尝试了我的水平,但我只能做到这一点。框架设置不正确,我不知道有没有其他简单的方法来做到这一点。

代码

- (void)viewDidLoad {
    [super viewDidLoad];
    UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
    [dragview addGestureRecognizer:gesture];
}


- (void)handleGesture:(UIPanGestureRecognizer *)gestureRecognizer
{
    CGPoint velocity = [gestureRecognizer velocityInView: gestureRecognizer.view];
    CGPoint location = [gestureRecognizer locationInView: gestureRecognizer.view];

    if(velocity.x > 0)
    {
        NSLog(@"gesture went right");
        if (dragview.frame.origin.x + dragview.frame.size.width >= dragV.frame.size.width) {
            [dragview setFrame:CGRectMake(dragV.frame.size.width - dragview.frame.size.width, dragview.frame.origin.y, dragview.frame.size.width, dragview.frame.size.height)];
            english.center = CGPointMake(dragV.frame.size.width  / 2,
                                                              dragV.frame.size.height / 2);
        } else {
            float dX = location.x-panCoord.x;
            gestureRecognizer.view.frame = CGRectMake(gestureRecognizer.view.frame.origin.x+dX, 0, gestureRecognizer.view.frame.size.width, gestureRecognizer.view.frame.size.height);
            [english setFrame:CGRectMake(english.frame.origin.x + 1.0f, english.frame.origin.y, english.frame.size.width, english.frame.size.height)];
            [arabic setFrame:CGRectMake(arabic.frame.origin.x + 1.0f, arabic.frame.origin.y, arabic.frame.size.width, arabic.frame.size.height)];
        }
    }
    else
    {
        NSLog(@"gesture went left");
        if (dragview.frame.origin.x <= 0) {
            [dragview setFrame:CGRectMake(0, dragview.frame.origin.y, dragview.frame.size.width, dragview.frame.size.height)];
            arabic.center = CGPointMake(dragV.frame.size.width  / 2,
                                         dragV.frame.size.height / 2);
        } else {
            float dX = location.x+panCoord.x;
            gestureRecognizer.view.frame = CGRectMake(gestureRecognizer.view.frame.origin.x+dX, 0, gestureRecognizer.view.frame.size.width, gestureRecognizer.view.frame.size.height);
            [english setFrame:CGRectMake(english.frame.origin.x - 1.0f, english.frame.origin.y, english.frame.size.width, english.frame.size.height)];
            [arabic setFrame:CGRectMake(arabic.frame.origin.x - 1.0f, arabic.frame.origin.y, arabic.frame.size.width, arabic.frame.size.height)];
        }
    }
}

截图

最初它看起来像这样,

enter image description here

拖着阿拉伯语,

enter image description here

虽然拖向英语, enter image description here

答案很感激!!

1 个答案:

答案 0 :(得分:1)

我得到了输出。我尝试了你的代码,我使用了SwipeGestureRecognizer代码。现在工作正常。

我试过2次选择

选项1:SwipeGestureRecognizer

- (void)viewDidLoad 
{
   [super viewDidLoad];

   // Swipe Left
   UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeft:)];
   swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
   [arabicView addGestureRecognizer:swipeLeft];

   // Swipe Right
   UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)];
   swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
   [englishView addGestureRecognizer:swipeRight];
}

- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)recognizer 
{
  [self performSelector:@selector(moveAtRight) withObject:nil afterDelay:0.01f];
}

- (void)handleSwipeRight:(UISwipeGestureRecognizer *)recognizer 
{
  [self performSelector:@selector(moveAtLeft) withObject:nil afterDelay:0.01f];
}


-(void)moveAtRight
{
  englishView.frame = CGRectMake(0, 0, 53, 53);
  englishView.backgroundColor = [UIColor lightGrayColor];
  arabicView.frame = CGRectMake(53, 0, 205, 53);
  english.text = @"English";
  arabic.text = @"Arabic";
  arabicView.backgroundColor = [UIColor blueColor];
  [dragview removeFromSuperview];
}


-(void)moveAtLeft
{
  englishView.frame = CGRectMake(0, 0, 205, 53);
  arabicView.frame = CGRectMake(205, 0, 53, 53);
  arabicView.backgroundColor = [UIColor lightGrayColor];
  englishView.backgroundColor = [UIColor blueColor];
  english.text = @"English";
  arabic.text = @"Arabic";
  [dragview removeFromSuperview];
}

在上面的代码中,我将背景颜色设置为蓝色。如果您想要任何其他颜色更改。

初始

enter image description here

当我向阿拉伯语滑动时

enter image description here

当我向英语滑动时

enter image description here

以上是输出。

选项2:PanGestureRecognizer

现在我尝试使用你的PanGestureRecognizer Code.I分别为englishView和arabicView设置框架和背景颜色。现在工作正常。

- (void)viewDidLoad 
{
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.

  _drawPath = [UIBezierPath
                          bezierPathWithRoundedRect:CGRectMake(0, 0, 140, 35)
                          byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight | UIRectCornerTopLeft | UIRectCornerTopRight)
                          cornerRadii:CGSizeMake(4, 4)
                          ];
  _rectLayer = [[CAShapeLayer alloc] init];
  _rectLayer.path = _drawPath.CGPath;
  _rectLayer.strokeColor = [UIColor blackColor].CGColor;
  _rectLayer.lineWidth = 2.0f;
  _rectLayer.fillColor = [UIColor clearColor].CGColor;
  _rectLayer.strokeEnd = 0.f;
  [vi.layer addSublayer:_rectLayer];

  [self drawRectangle:nil];

  UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
  [gesture setMinimumNumberOfTouches:1];
  [gesture setMaximumNumberOfTouches:1];
  [englishView addGestureRecognizer:gesture];

  UIPanGestureRecognizer *gesture1 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
  [gesture1 setMinimumNumberOfTouches:1];
  [gesture1 setMaximumNumberOfTouches:1];
  [arabicView addGestureRecognizer:gesture1];
}

- (void)handleGesture:(UIPanGestureRecognizer *)gestureRecognizer
{

   if (gestureRecognizer.state == UIGestureRecognizerStateChanged)
   {
      NSLog(@"Changed");
      CGPoint velocity = [gestureRecognizer velocityInView:gestureRecognizer.view];
      CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view];

      if(velocity.x > 0)
      {
            NSLog(@"gesture went right");
            gestureRecognizer.view.frame = CGRectMake(gestureRecognizer.view.frame.origin.x + translation.x, 0, gestureRecognizer.view.frame.size.width, gestureRecognizer.view.frame.size.height);
            NSLog(@"The gestureRecognizer frame is - %@",NSStringFromCGRect(gestureRecognizer.view.frame));
            [englishView setFrame:CGRectMake(0, englishView.frame.origin.y, 205, englishView.frame.size.height)];
            [englishView setBackgroundColor:[UIColor colorWithRed:(15/255.0f) green:(97/255.0f) blue:(163/255.0f) alpha:1.0f]];
            [arabicView setFrame:CGRectMake(205, arabicView.frame.origin.y, 53, arabicView.frame.size.height)];
            [arabicView setBackgroundColor:[UIColor lightGrayColor]];
            [gestureRecognizer setTranslation:CGPointMake(0, 0) inView:dragV];
            [dragview removeFromSuperview];

    }
    else
    {
            NSLog(@"gesture went left");
            gestureRecognizer.view.frame = CGRectMake(gestureRecognizer.view.frame.origin.x + translation.x, 0, gestureRecognizer.view.frame.size.width, gestureRecognizer.view.frame.size.height);
            [arabicView setFrame:CGRectMake(53, arabicView.frame.origin.y, 205, arabicView.frame.size.height)];
            [englishView setFrame:CGRectMake(0, englishView.frame.origin.y,53,englishView.frame.size.height)];
            [arabicView setBackgroundColor:[UIColor colorWithRed:(15/255.0f) green:(97/255.0f) blue:(163/255.0f) alpha:1.0f]];
            [englishView setBackgroundColor:[UIColor lightGrayColor]];
            [gestureRecognizer setTranslation:CGPointMake(0, 0) inView:dragV];
            [dragview removeFromSuperview];
    }
  }
}

首先运行应用

enter image description here

中指向阿拉伯语观看

enter image description here

中指向英语视图

enter image description here