繁体   English   中英

UIView animation 物镜从左向右滑动后从右向左滑动的侧视图 c

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

我正在尝试制作侧边菜单。 在使用 CGRectOffset 然后在 window 上添加另一个视图并使其从左向右滑动后,我设法找出如何将主视图向右滑动。 它有效,但我怎样才能让主视图和另一个视图向左滑动? 任何帮助表示赞赏

- (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];
            
          
         }];
}

您可以以相同的方式使用 animation 个块。 您可以在 UIView animation 块中更改视图的框架。

#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

访问属性时使用Dot notation而不是Arrow operator

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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