Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
8
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

@khashi001

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

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
8
Help us understand the problem. What are the problem?