简体   繁体   中英

UIView animation side view slide from right to left after sliding from left to right in objective c

I am trying to make a side menu. I manage to find out how to slide to the right the mainview after using CGRectOffset and then adding another view on window and make it slide from left to right. It works but how can i make the mainview and the other view slide to left? Any help appreciated

- (IBAction)showsideview:(id)sender {
    NSLog(@"sideview button pushed");
    
 
    [UIView animateWithDuration:0.5f animations:^{
        self.view.frame = CGRectOffset(self.view.frame, 243, 0);
       
        [ self.view.layer setShadowColor:[UIColor whiteColor].CGColor];
              [ self.view.layer setShadowOpacity:0.8];
              [ self.view.layer setShadowRadius:3.0];
              [ self.view.layer setShadowOffset:CGSizeMake(-2.0, -2.0)];
    }];
        
          sample = [self.storyboard instantiateViewControllerWithIdentifier:@"side"];
    
                  self->sample.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
   
          [UIView animateWithDuration:0.5f animations:^{
        
              [self.view.window addSubview:self->sample.view];
            
          
         }];
}

You can use animation blocks in the same way. You can change the frame of the view within the UIView animation block.

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIView *sideView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configureView];
}

#pragma mark - Configure View

- (void)configureView {
    [self configureSideView];
}

- (void)configureSideView {
    CGRect frame = CGRectMake(-self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
    self.sideView = [[UIView alloc] initWithFrame:frame];
    self.sideView.backgroundColor = UIColor.redColor;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake((self.view.frame.size.width - 100) / 2, (self.view.frame.size.height - 100) / 2, 100, 100);
    [button setTitle:@"button" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(sideViewButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
    [self.sideView addSubview:button];
    
    [self.view addSubview:self.sideView];
}

#pragma mark - Button Action

- (IBAction)mainViewButtonTouched:(id)sender {
    [UIView animateWithDuration:1 animations:^{
        self.sideView.frame = self.view.bounds;
    }];
}

- (void)sideViewButtonTouched:(id)sender {
    [UIView animateWithDuration:1 animations:^{
        self.sideView.frame = CGRectMake(-self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
    }];
}

@end

Use Dot notation rather than Arrow operator when accessing property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM