xcode 如何使用 UISwipeGestureRecognizer 为左/右滑动视图设置动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25622692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:28:30  来源:igfitidea点击:

How to animate left/right swipe view using UISwipeGestureRecognizer

iosxcodeanimationios7uiswipegesturerecognizer

提问by Ujesh Patel

try to make swipe in single-view and it's working but not get animation and page indicatore bubble in bottom

尝试在单视图中进行滑动并且它正在工作但没有在底部获得动画和页面指示器气泡

- (void)viewDidLoad
{
[super viewDidLoad];
UISwipeGestureRecognizer *swipeleft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(screenswipeleft)];
swipeleft.numberOfTouchesRequired=1;
swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeleft];

UISwipeGestureRecognizer *swiperight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(viewDidLoad)];
swiperight.numberOfTouchesRequired=1;
swiperight.direction=UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swiperight];

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
[self.view addSubview:textField];
NSLog(@"move left");

}
-(void)screenswipeleft {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
[self.view addSubview:textField];
NSLog(@"move right");

}

采纳答案by Ganesh Kumar

- (void)viewDidLoad
  {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.   
    _leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
    _rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];

    _leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    _rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;

    [self.view addGestureRecognizer:_leftSwipeGestureRecognizer];
    [self.view addGestureRecognizer:_rightSwipeGestureRecognizer];
}

- (void)handleSwipes:(UISwipeGestureRecognizer *)sender
 {
    if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
    CGPoint labelPosition = CGPointMake(self.swipeLabel.frame.origin.x - 100.0, self.swipeLabel.frame.origin.y);
    self.swipeLabel.frame = CGRectMake( labelPosition.x , labelPosition.y , self.swipeLabel.frame.size.width, self.swipeLabel.frame.size.height);

    }

    if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
        CGPoint labelPosition = CGPointMake(self.swipeLabel.frame.origin.x + 100.0, self.swipeLabel.frame.origin.y);
        self.swipeLabel.frame = CGRectMake( labelPosition.x , labelPosition.y , self.swipeLabel.frame.size.width, self.swipeLabel.frame.size.height);
   }
}

回答by Saheb Roy

Suppose you want to animate the UITextField. And your UITextField has a CGRect with starting point of x - 50, y - 100;

假设您要为 UITextField 设置动画。你的 UITextField 有一个 CGRect,起点为 x - 50, y - 100;

   @implementation myViewController{
   UITextField *myTextField; //Declaring a textfield as an instance variable (global).
 }


    -(void)viewDidLoad{
    myTextField = [UITextField alloc]initWithFrame:CGRectMake(50,100,100,50)];//adding memory and setting its initial position
    //your other codes.
}
   -(void)screenswipeleft {
   [UIView animateWithDuration:3 animations:^{
        myTextField.frame = CGRectMake:(-75,100,100,50); //setting the same textfield in a position of -75 only showing 1/4th of the textfield. 
//this block animation would make the textfield animate from its starting position to this position in 3 seconds. (animateWithDuration:3 here u mention its duration).
    }];
   }

Apply same logic to others and you will get the animation you want.

将相同的逻辑应用于其他人,您将获得所需的动画。

回答by Amrendra Pratap Singh

Try this.

尝试这个。

-(void)screenswipeleft 
{
   [UIView beginAnimations:nil context:nil];
                       [UIView setAnimationDuration:0.4];

                       textField.frame = CGRectMake(10, 200, 300, 40)];


                       [UIView commitAnimations]



}