? 在寫一個項目用RESIDEMENU纽窟,這是寫得很好的一個開源組件填具,只需要幾行代碼就可以使用。所以現(xiàn)在抽空看看代碼米奸,看看作者是怎么實現(xiàn)的昼接。
- (void)awakeFromNib {
if (self.contentViewStoryboardID) {
self.contentViewController = [self.storyboard instantiateViewControllerWithIdentifier:self.contentViewStoryboardID];
}
if (self.leftMenuViewStoryboardID) {
self.leftMenuViewController = [self.storyboard instantiateViewControllerWithIdentifier:self.leftMenuViewStoryboardID];
}
if (self.rightMenuViewStoryboardID) {
self.rightMenuViewController = [self.storyboard instantiateViewControllerWithIdentifier:self.rightMenuViewStoryboardID];
}
}
? 這是.m文件里的第一個方法爽篷,很好理解悴晰,這是通過storyboard設好ID,然后通過ID初始化三個vc,分別作為內(nèi)容試圖铡溪、左視圖漂辐、右視圖。
- (void)viewDidLoad {
[super viewDidLoad];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.backgroundImageView = ({
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
imageView.image = self.backgroundImage;
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
imageView;
});
self.contentButton = ({
UIButton *button = [[UIButton alloc] initWithFrame:CGRectNull];
[button addTarget:self action:@selector(hideMenuViewController) forControlEvents:UIControlEventTouchUpInside];
button;
});
[self.view addSubview:self.backgroundImageView];
[self.view addSubview:self.menuViewContainer];
[self.view addSubview:self.contentViewContainer];
//重點
self.menuViewContainer.frame = self.view.bounds;
self.menuViewContainer.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
if (self.leftMenuViewController) {
[self addChildViewController:self.leftMenuViewController];
self.leftMenuViewController.view.frame = self.view.bounds;
self.leftMenuViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.menuViewContainer addSubview:self.leftMenuViewController.view];
[self.leftMenuViewController didMoveToParentViewController:self];
}
if (self.rightMenuViewController) {
[self addChildViewController:self.rightMenuViewController];
self.rightMenuViewController.view.frame = self.view.bounds;
self.rightMenuViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.menuViewContainer addSubview:self.rightMenuViewController.view];
[self.rightMenuViewController didMoveToParentViewController:self];
}
self.contentViewContainer.frame = self.view.bounds;
self.contentViewContainer.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self addChildViewController:self.contentViewController];
self.contentViewController.view.frame = self.view.bounds;
[self.contentViewContainer addSubview:self.contentViewController.view];
[self.contentViewController didMoveToParentViewController:self];
self.menuViewContainer.alpha = !self.fadeMenuView ?: 0;
if (self.scaleBackgroundImageView)
self.backgroundImageView.transform = CGAffineTransformMakeScale(1.7f, 1.7f);
[self addMenuViewControllerMotionEffects];
if (self.panGestureEnabled) {
self.view.multipleTouchEnabled = NO;
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognized:)];
panGestureRecognizer.delegate = self;
[self.view addGestureRecognizer:panGestureRecognizer];
}
[self updateContentViewShadow];
}
? 這是viewdidload里面的代碼棕硫,可以看看“重點”開始的代碼髓涯,如果左視圖和右視圖存在就添加到當前視圖。內(nèi)容視圖是一定要有的哈扮。其實在看代碼前纬纪,沒想過vc添加vc的,之前都是用view添加view滑肉,用得多了包各,所以思維就有點固化了。RESIDEMENU原理其實很簡單靶庙,但是如果要做到作者的水平问畅,必須對vc間的關系、動畫六荒、手勢有相當?shù)姆e累才可以