English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

iOSでどうやって100行のコードでシンプルなドロワー効果を実現

前略

iOSでのドローウェルの簡単な実現は、多くのアプリケーションで使用されており、オンラインでも多くの例があります。この記事では、簡単なコードを使用して実現しています。必要な場合は、一緒に学習してください。

以下は画像の効果です

以下はサンプルコードです

#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController
+ (instancetype)mainViewControllerWithLeftViewController:(UIViewController *)leftViewController withMainPageViewController:(UIViewController *)mainPageViewController;
@end
#import "MainViewController.h"
#define KWidth self.view.frame.size.width
#define KHeight self.view.frame.size.height
@interface MainViewController ()
@property (nonatomic,strong)UIViewController *leftVC;
@property (nonatomic,strong)UIViewController *centerVC;
@property (nonatomic,assign)BOOL isSlider;
@property (nonatomic,strong)UIView *corverView;
@end
@implementation MainViewController
+ (instancetype)mainViewControllerWithLeftViewController:(UIViewController *)leftViewController withMainPageViewController:(UIViewController *)mainPageViewController{
  MainViewController *mainVC = [[MainViewController alloc] init];
  mainVC.leftVC = leftViewController;
  mainVC.centerVC = mainPageViewController;
  return mainVC;
}
- (void)viewDidLoad{
  [super viewDidLoad];
  self.isSlider = NO;
  self.view.backgroundColor = [UIColor whiteColor];
  [self addChildViewController:self.leftVC];
  [self.view addSubview:self.leftVC.view];
  [self addChildViewController:self.centerVC];
  [self.view addSubview:self.centerVC.view];
  // 左視図とメイン視図にジェスチャーを追加します
  [self addGestureForView];
}
// 主ビューに覆いを追加
- (void)addCorverView{
  if (self.corverView) {
    [self.corverView removeFromSuperview];
    self.corverView = nil;
  }
  self.corverView = [[UIView alloc] initWithFrame:self.centerVC.view.bounds];
  _corverView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
  [self.centerVC.view addSubview:self.corverView];
}
// 主ビューの覆いを削除
- (void)removeCoverView{
  if (self.corverView) {
    [self.corverView removeFromSuperview];
    self.corverView = nil;
  }
}
// 左視図とメイン視図にジェスチャーを追加します
- (void)addGestureForView{
  UISwipeGestureRecognizer *rightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction:)];
  rightGesture.direction = UISwipeGestureRecognizerDirectionRight;
  [self.centerVC.view addGestureRecognizer:rightGesture];
  UISwipeGestureRecognizer *leftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction:)];
  leftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
  [self.centerVC.view addGestureRecognizer:leftGesture];
  UISwipeGestureRecognizer *leftVCLeftSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftVCLeftSwipeAction:)];
  leftVCLeftSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
  [self.leftVC.view addGestureRecognizer:leftVCLeftSwipeGesture];
}
- (void)swipeRightAction:(id)sender{
  [self moveView:self.centerVC.view scale:0.8 panValue:KWidth];
  self.isSlider = YES;
  [self addCorverView];
}
- (void)swipeLeftAction:(id)sender{
  [self moveView:self.centerVC.view scale:1 panValue:KWidth / 2];
  self.isSlider = NO;
  [self removeCoverView];
}
- (void)leftVCLeftSwipeAction:(id)sender{
  [self moveView:self.centerVC.view scale:1 panValue:KWidth / 2];
  self.isSlider = NO;
  [self removeCoverView];
}
// ビューの平移と拡大
- (void)moveView:(UIView *)view scale:(CGFloat)scale panValue:(CGFloat)value{
  [UIView beginAnimations:nil context:nil];
  view.transform = CGAffineTransformScale(CGAffineTransformIdentity,scale,scale);
  view.center = CGPointMake(value, view.center.y);
  [UIView commitAnimations];
}
@end

まとめ

これでこの記事の全ての内容が終わりました。本文の内容が皆様の学習や仕事に役立つことを願っています。何かご不明な点があれば、コメントを残して交流してください。

声明:本文の内容はインターネットから取得しており、著作権者は所有者であり、インターネットユーザーが自発的に貢献し、自己でアップロードしたものであり、本サイトは所有権を持ちません。また、人工的な編集は行われておらず、関連する法的責任も負いません。著作権に問題があると感じた場合は、以下のメールアドレスまでご連絡ください:notice#oldtoolbag.com(メールを送信する際、#を@に置き換えてください。通報を行い、関連する証拠を提供してください。一旦確認が取れましたら、本サイトは即座に侵害を疑われるコンテンツを削除します。)

基本教程
おすすめ