我有一個水平分頁UIScrollView
建立與LXPagingViews
類的幫助(方便地添加一個「偷窺」 UIScrollView
)。這是設置的,所以你可以在UIScrollView
的不同視圖之間滑動。我爲每個視圖添加了UIPinchGestureRecognizer
,因此當用戶捏在視圖上時,它會顯示「縮放」動畫,然後進入詳細視圖。一種用於更清晰的屏幕截圖:
問題:
當我捏的意見,變焦動畫開始一個和所述視圖繼續入詳細視圖。這工作正常,除了在UIScrollView
的最後一個視圖。在最後一個視圖(無論它有多少個視圖,只要它有2個以上視圖無關),當動畫播放時,它會在我拍攝的視圖背後產生一個新視圖,併爲其創建動畫。這使得一個奇怪的動畫。此外,該視圖不會被動畫輸入阻止,因爲它產生了一個新的視圖。這意味着用戶可以通過快速捏造來快速生成多個視圖。
我希望這張截圖說明了這個問題有點:
我曾嘗試:
- 調試手勢識別的上海華子視圖,檢查是否有更多的意見產卵
- 監視哪些視圖獲得了動畫效果。對於用戶的每一次捏,另一個視圖由於某種原因得到動畫
- 試圖使用另一種方式設置圖像,而不是延遲加載。沒有幫助
- 嘗試了其他圖像,越來越少的意見,方向差異和空視圖。
代碼:
添加手勢識別的觀點:
- (UIView<ReusableView> *)pagingView:(PagingView *)thePagingView reusableViewForPageIndex:(NSUInteger)thePageIndex withFrame:(CGRect)theFrame {
// View's Identifier
static NSString *theIdentifier = @"voucherDetailView";
VoucherDetailView *thePageView = (VoucherDetailView *)[thePagingView dequeueReusableViewWithIdentifier:theIdentifier];
if (thePageView == nil) {
thePageView = [[VoucherDetailView alloc] initWithFrame:theFrame];
} else {
thePageView.frame = theFrame;
}
Voucher *voucher = [_vouchers objectAtIndex:thePageIndex];
NSURL *fullVoucherImageUrl = [NSURL URLWithString:voucher.fullVoucherImage];
[thePageView.voucher setImageWithURL:fullVoucherImageUrl placeholderImage:[UIImage imageNamed:@"11283253-nederland_kaart"]];
// Gesture recognizer
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(openZoomablePinch:)];
[thePageView addGestureRecognizer:pinchGesture];
return thePageView;
}
捏處理程序:
- (void)openZoomablePinch:(UIPinchGestureRecognizer*)sender {
// Only allow one pinch to activate the animation
if (sender.state == UIGestureRecognizerStateBegan) {
// Bring the view to the front so it doesn't clip with the peeping views
[sender.view.superview bringSubviewToFront:sender.view];
CGAffineTransform trans = sender.view.transform;
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveEaseOut
animations:^{
sender.view.transform = CGAffineTransformScale(sender.view.transform, 1.15, 1.15);
}
completion:^(BOOL finished) {
if (finished && self.view.window) {
sender.view.transform = trans;
[self performSegueWithIdentifier:@"voucherZoomSegue" sender:self];
}
}];
}
}
編輯:PagingView代碼被調用的捏時的最後視圖:
UIView<ReusableView> *theRightMostReusableView = [self.visibleReusableViews lastObject];
NSUInteger theRightMostPageIndex = (NSUInteger)floorf(CGRectGetMinX(theRightMostReusableView.frame)/thePageWidth);
while ((theRightMostPageIndex != MAX(0, self.numberOfItems - 1)) && (theRightMostPageIndex < theToIndex)) {
theRightMostPageIndex = MIN(theRightMostPageIndex + 1, MAX(0, self.numberOfItems - 1));
CGFloat theMinX = theRightMostPageIndex * thePageWidth;
CGRect theRect = CGRectMake(theMinX, 0.0f, thePageWidth, thePageHeight);
UIView<ReusableView> *theReusableView = [self.dataSource pagingView:self reusableViewForPageIndex:theRightMostPageIndex withFrame:theRect];
if (!CGRectContainsRect(theRect, theReusableView.frame)) {
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:[NSString stringWithFormat:
@"theReusableView's frame (%@) must be contained by the given frame (%@)",
NSStringFromCGRect(theReusableView.frame),
NSStringFromCGRect(theRect)]
userInfo:nil];
}
[self.visibleReusableViews addObject:theReusableView];
[self addSubview:theReusableView];
}
編輯2:
我已上傳一個小演示項目here。使用它的方式是,將模擬器(或您的設備)設置爲橫向模式,轉到Peeping Paging View,然後嘗試捏取/點擊視圖(第一個視圖除外)。正如你所看到的那樣,除了最後一個視圖之外,這些視圖會被縮放,這樣會產生一個新的視圖,並放大一個視圖('9'保持相同的大小)。代碼PeepingPagingViewController.m
。
溶液(多虧Oladya凱恩):
最右邊的可見索引mistakingly索引爲最左邊的可見索引。我不得不改變代碼是行:
NSUInteger theRightMostPageIndex = (NSUInteger)floorf(CGRectGetMinX(theRightMostReusableView.frame)/thePageWidth);
到
NSUInteger theRightMostPageIndex = MIN((NSInteger)floorf((CGRectGetMaxX(theRightMostReusableView.frame) - 0.1f)/thePageWidth), MAX(0, self.numberOfItems - 1));
你是否在檢查最後一個視圖時檢查是否調用了'reusableViewForPageIndex'?如果是,您的ScrollView類的手勢出現問題。 – danypata
好點!當我捏住最後一個視圖時,它確實會被調用,並且不會被其他視圖調用。你有什麼想法,我應該看看ScrollView類嗎?感謝您的幫助! – Thermometer
檢查自定義視圖控制器中調用了'reusableViewForPageIndex'的哪個位置,並查看調用此方法的條件,某些情況是錯誤的,這就是爲什麼它創建另一個視圖。還要確保'dataSource'是正確的(沒有比它應該更多的視圖) – danypata