LoginSignup
8
8

More than 5 years have passed since last update.

ScrollViewと Swipe Gesture Recognizer が両方動くようにする方法

Posted at

ScrollView上で Swipe Gesture Recognizerが動かなくてハマったので、解決方法をメモします。

前提:

・iOS9.2, Xcode 7.1.1 環境です。
・Swipe Gesture Recognizerの設置はStoryboardを使って次のように行いました。
 1. StoryBoardで View Controller のViewの中にScrollViewを設置。
 2. ScrollViewの中にViewを設置。=> ContentViewと命名。
 3. ContentView に、Swipe Gesture Recognizerを設置。


解決方法:

1. Storyboardで ViewController.h にScrollViewのIBOutletを定義する

StoryboardでScrollViewを選択し、Controllキーを押しながらViewController.h にドラッグして、mySctollViewと命名。

ViewController.h:
@property (weak, nonatomic) IBOutlet UIScrollView *myScrollView;

2. ViewController.h にDelegateの定義を追加する

2つ書く。
・UIScrollViewDelegate
・UIGestureRecognizerDelegate

@interface SettingViewController : UIViewController <UIScrollViewDelegate, UIGestureRecognizerDelegate>

3. ViewController.m のviewDidLoadに delegate宣言を1行追加する

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // Scroll View setting
    self.myScrollView.delegate = self; // <= ★★★ ここ ★★★

4. ViewController.m に以下のメソッドを追加する

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}

これで ScrollView内の Pan Gesture Recognizerと Swipe Gesture Recognizer の両方が動くようになりました。

参考にしたサイトはこちらです。ありがとうございます。
http://d.hatena.ne.jp/Mitsuyoshi/20131118/1384753107
http://stackoverflow.com/questions/23841045/how-to-have-a-uiscrollview-scroll-and-have-a-gesture-recognizer
http://stackoverflow.com/questions/6425785/uigesturerecognizer-over-uiscrollview

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8