2014-10-03 72 views
0

以下是我使用UIScrollView使用AutoLayout啓用分頁的App Tour代碼(Masonry)它在Portrait中正常工作,但當我旋轉分頁時被禁用,因此它在Landscape中不起作用。然後它在縱向模式下也不起作用。有人可以幫助這裏有什麼問題嗎?UIScrollView頁面旋轉後禁用

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.view.backgroundColor = [UIColor clearColor]; 

    self.scrollView = [UIScrollView new]; 
    self.scrollView.backgroundColor = [UIColor clearColor]; 
    self.scrollView.scrollEnabled = YES; 
    self.scrollView.pagingEnabled = YES; 
    self.scrollView.delegate = self; 
    self.scrollView.showsHorizontalScrollIndicator = NO; 
    [self.view addSubview:self.scrollView]; 

    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.edges.equalTo(self.view); 
    }]; 

    UIImageView *esImageview1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CommonResources.bundle/page1"]]; 
    esImageview1.contentMode = UIViewContentModeScaleAspectFit; 
    esImageview1.backgroundColor = [UIColor clearColor]; 
    esImageview1.userInteractionEnabled = YES; 
    [self.scrollView addSubview:esImageview1]; 

    [esImageview1 mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.left.top.bottom.equalTo(self.scrollView); 
     make.width.height.equalTo(self.view); 
    }]; 

    UIImageView *esImageview2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CommonResources.bundle/page2"]]; 
    esImageview2.contentMode = UIViewContentModeScaleAspectFit; 
    esImageview2.backgroundColor = [UIColor clearColor]; 
    esImageview2.userInteractionEnabled = YES; 
    [self.scrollView addSubview:esImageview2]; 

    [esImageview2 mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.left.equalTo(esImageview1.mas_right); 
     make.top.bottom.equalTo(self.scrollView); 
     make.width.height.equalTo(self.view); 
    }]; 

    UIImageView *esImageview3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CommonResources.bundle/page3"]]; 
    esImageview3.contentMode = UIViewContentModeScaleAspectFit; 
    esImageview3.backgroundColor = [UIColor clearColor]; 
    esImageview3.userInteractionEnabled = YES; 
    [self.scrollView addSubview:esImageview3]; 

    [esImageview3 mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.left.equalTo(esImageview2.mas_right); 
     make.top.bottom.equalTo(self.scrollView); 
     make.width.height.equalTo(self.view); 
    }]; 

    self.pageControl = [UIPageControl new]; 
    self.pageControl.numberOfPages = 3; 
    self.pageControl.currentPage = 0; 
    self.pageControl.backgroundColor = [UIColor clearColor]; 
    [self.view addSubview:self.pageControl]; 

    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.left.right.equalTo(self.view); 
     make.height.equalTo(@40); 
     make.top.equalTo(self.view).with.offset(40); 
    }]; 

    UILabel *titleLabel = [UILabel new]; 
    titleLabel.text = @"App Tour"; 
    titleLabel.backgroundColor = [UIColor clearColor]; 
    titleLabel.textColor = [UIColor whiteColor]; 
    titleLabel.font = [UIFont fontWithName:@"Helvetica-Light" size:16]; 
    titleLabel.textAlignment = NSTextAlignmentCenter; 
    [self.view addSubview:titleLabel]; 

    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.left.right.equalTo(self.view); 
     make.height.equalTo(@40); 
     make.top.equalTo(self.view).with.offset(20); 
    }]; 

    UIButton *doneButton = [UIButton new]; 
    [doneButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Light" size:16]]; 
    [doneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
    [doneButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted]; 
    [doneButton setTitle:@"Done" forState:UIControlStateNormal]; 
    [doneButton addTarget:self action:@selector(doneButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    doneButton.backgroundColor = [UIColor clearColor]; 
    [self.view addSubview:doneButton]; 

    [doneButton mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.right.equalTo(self.view); 
     make.height.equalTo(@40); 
     make.width.equalTo(@70); 
     make.top.equalTo(self.view).with.offset(20); 
    }]; 
} 

- (void)doneButtonPressed:(id)sender { 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

- (void)viewDidLayoutSubviews { 
    [super viewDidLayoutSubviews]; 
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width*self.pageControl.numberOfPages, self.view.frame.size.height); 
} 

- (void)scrollViewDidScroll:(UIScrollView *)sender { 
    CGFloat pageWidth = self.scrollView.frame.size.width; 
    int page = floor((self.scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1; 
    self.pageControl.currentPage = page; 
} 

Scrollview after rotation

回答

0

我只是想重置滾動型在didRotateFromInterfaceOrientation委託contentsize。不知道爲什麼,因爲當我旋轉時,videDidLayoutSubview已被調用,我已經重新設置內容大小。

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width*self.pageControl.numberOfPages, self.view.frame.size.height); 
} 
0

我猜另一個視圖或類似的東西得到它掩蓋起來。嘗試繼承UIScrollView並實現以下方法來檢查它是否響應您的觸摸。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    NSLog(@"OMG TOUCHES WORK, NOW WHAT?"); 
} 

你可以很容易地檢查它是否被強迫它置​​於前面。在包含UIScrollView的UIViewController中,在回調中調用這行代碼。它應該解決問題。您可能必須繼承控制器的UIView,並將此方法放在didLayoutSubviews方法中,而不是十分確定,取決於您的實現。

[self.view bringSubViewToFront:self.scrollView]; 

旋轉前後還要檢查框架和邊界,看看是否發現任何異常的地方尋找

+0

我檢查過沒有任何內容是覆蓋scrollview。在最上面,我有每頁圖像。當我旋轉scrollview自動滾動到第一頁,然後停止分頁並且永不回頭時,這是非常奇怪的。 – applefreak 2014-10-03 22:33:39

+0

有沒有機會頂級imageview失去交互啓用屬性?我在旋轉後添加了圖像 - 注意到我在頂部的scrollview中有一個imageview,這是預期的。 – applefreak 2014-10-03 22:40:38

+0

它不應該在我的知識中失去啓用的屬性。你有沒有嘗試子類化和實現觸摸方法。因爲這會告訴你很多。另外一個非常好的新功能我喜歡(如果您使用xCode 6,您應該)新的「查看調試」功能。你可以在這裏閱讀更多, https://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/WhatsNewXcode/Articles/xcode_6_0.html – anders 2014-10-06 15:43:23