【问题标题】:UIView animation side view slide from right to left after sliding from left to right in objective cUIView动画侧视图在目标c中从左向右滑动后从右向左滑动
【发布时间】:2021-11-28 18:23:32
【问题描述】:

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

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

【问题讨论】:

  • 您可以按照刚才的方式进行操作:在动画块中设置帧。

标签: ios objective-c uiviewanimation


【解决方案1】:

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

#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

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-10
  • 1970-01-01
  • 2011-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-19
相关资源
最近更新 更多