Yash Joshi
Yash Joshi

Reputation: 199

UIView having Animation from right to left

how to make uiview animated so it appears slowly from right to left and having two UIButtons on it having images on buttons and one of them image on button will change on button click

Upvotes: 1

Views: 9104

Answers (4)

Jack
Jack

Reputation: 14329

Right to Left animation & Left to right animation as-

        let firstViewTowardsLeft = UIView(frame: CGRect(x: 0, y: 150, width: 50, height: 50))
        firstViewTowardsLeft.backgroundColor = .red
        view.addSubview(firstViewTowardsLeft)
        UIView.animate(withDuration: 1, delay: 0, options: .repeat, animations: {Bool in
            firstViewTowardsLeft.frame = CGRect(x: 100, y: 150, width: 50, height: 50)

        }, completion: nil)


        let secondViewTowardsLeft = UIView(frame: CGRect(x: 100, y: 100, width: 50, height: 50))
        secondViewTowardsLeft.backgroundColor = .red
        view.addSubview(secondViewTowardsLeft)
        UIView.animate(withDuration: 1, delay: 0, options: .repeat, animations: {Bool in
            secondViewTowardsLeft.frame = CGRect(x: 0, y: 100, width: 50, height: 50)

        }, completion: nil)

https://developer.apple.com/documentation/uikit/uiview/1622418-animate

Upvotes: 0

Yash Joshi
Yash Joshi

Reputation: 199

[UIView animateWithDuration:0.7f
                 animations:^ {
                     CGRect frame = YourView.frame;
                     frame.origin.x = 0;
                     YourView.frame = frame;
                     YourView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);
                 }
                 completion:^(BOOL finished) {

                     [UIView beginAnimations:nil context:nil];
                     [UIView setAnimationDuration:5.3];
                     [UIView commitAnimations];
                 }];

Upvotes: 3

amit gupta
amit gupta

Reputation: 1167

You can try this code and make changes according to your requirements . here i create a baseview which moves from right to left and vice versa. In that baseview you put whatever u want to animate. set bounceDistance,bounceDuration,animateWithDuration,delay variable according to your requirement.

-(IBAction)rightClicked:(id)sender {
    CGRect initalFrame = self.baseView.frame;
    initalFrame.origin.x = initalFrame.size.width;
   self.baseView.frame = initalFrame;
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(moveFromLeftOrRight:) userInfo:@0 repeats:NO];
  }

-(IBAction)leftClicked:(id)sender {
   CGRect initalFrame = self.baseView.frame;
   initalFrame.origin.x = -initalFrame.size.width;
   self.baseView.frame = initalFrame;
   [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(moveFromLeftOrRight:) userInfo:@1 repeats:NO];
  }

-(void)moveFromLeftOrRight:(NSTimer *) timer {
    BOOL isLeft = [timer.userInfo boolValue];
    CGFloat bounceDistance = 10;
    CGFloat bounceDuration = 0.2;
    [UIView animateWithDuration:.2 delay:0.0 options:UIViewAnimationOptionAllowAnimatedContent
    animations:^{
     CGFloat direction = (isLeft ? 1 : -1);
     self.baseView.center = CGPointMake(self.baseView.frame.size.width/2 + direction*bounceDistance, self.baseView.center.y);}
    completion:^(BOOL finished){
     [UIView animateWithDuration:bounceDuration animations:^{ 
    self.baseView.center = CGPointMake(self.baseView.frame.size.width/2, self.baseView.center.y);
    }];
 }];
}

Upvotes: 1

Kaushil Ruparelia
Kaushil Ruparelia

Reputation: 1178

Assuming you are targeting iPhone.

  • Set the view's x co ordinate to, say 320(max visible in iPhone) in the storyboard (IB).

    x-320 y-0 width-320 height-568

  • In the viewDidAppear: you can animate and bring the view to the starting co ordinate, say 0. You can do this as follows

    -(void)viewDidAppear:(BOOL)animated {
         [super viewDidAppear:animated];
    
         [UIView animateWithDuration:10.f animations:^{
              self.view.frame = CGRectMake(0.f, 0.f, 320.f, 568.f);
         }];
    }
    

For the buttons you will have to be specific.

Upvotes: 0

Related Questions