UIView动画侧视图在目标c中从左向右滑动后从右向左滑动



我正在尝试制作一个侧菜单。我设法找出如何在使用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];


}];
}

你可以用同样的方式使用动画块。你可以在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

最新更新