如何測試滾動視圖是否彈起?彈跳結束時是否有通知?如何測試滾動視圖是否彈起?
8
A
回答
5
是的......檢查出UIScrollViewDelegate規範,實現包括以下兩個方法,並據此設置UIScrollView的委託:
// User stops dragging the table view.
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView
willDecelerate:(BOOL)decelerate;
// Control slows to a halt after the user drags it
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
你可能最感興趣的scrollViewDidEndDecelerating。這些也在UITableView中工作,這是我最初找到它們的地方(UITableView繼承自UIScrollView)。
28
這是我如何檢測如果滾動視圖水平反彈:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.contentOffset.x < 0) {
NSLog(@"bouncing left");
}
if (scrollView.contentOffset.x > (scrollView.contentSize.width - scrollView.frame.size.width)) {
NSLog(@"bouncing right");
}
}
8
輕微修訂Justin的方法,允許contentInset:
if(scrollView.contentOffset.x < -scrollView.contentInset.left)
{
NSLog(@"bounce left");
}
if(scrollView.contentOffset.x > scrollView.contentSize.width - scrollView.frame.size.width + scrollView.contentInset.right)
{
NSLog(@"bounce right");
}
1
對於那些你將能夠誰以在滾動視圖中向下「反彈」以更新視圖的內容。 (而該事件僅觸發一次。)
- (void)scrollViewDidEndDragging:(UIScrollView *)aScrollView
willDecelerate:(BOOL)decelerate{
CGPoint offset = aScrollView.contentOffset;
CGRect bounds = aScrollView.bounds;
CGSize size = aScrollView.contentSize;
UIEdgeInsets inset = aScrollView.contentInset;
float y = offset.y + bounds.size.height - inset.bottom;
float h = size.height;
//Distance in points before update-event occur
float reload_distance = 50;
//
if(y > h + reload_distance) {
NSLog(@"load more rows");
}
}
1
老問題,但我只是碰到了類似的問題,並想補充一點,這是一個好主意,還檢查滾動視圖內容大於滾動大視圖的框架:
+ (BOOL) isScrollViewBouncing:(UIScrollView *)scrollView
{
return scrollView.contentOffset.y > scrollView.contentSize.height - scrollView.frame.size.height
&& scrollView.contentSize.height > scrollView.frame.size.height;
}
現在,這確保滾動視圖是大到足以在滾動彈跳的狀態,因此,如果滾動視圖小它並不總是爲true。
乾杯
0
使用Glavid的回答,我說反彈至底部還,並添加爲一個類別
#import "UIScrollView+Additions.h"
@implementation UIScrollView (Additions)
- (BOOL)isBouncing
{
BOOL isBouncingBottom = self.contentOffset.y >= self.contentSize.height - self.frame.size.height
&& self.contentSize.height >= self.frame.size.height;
BOOL isBouncingTop = self.contentOffset.y <= 0;
BOOL isBouncing = isBouncingBottom || isBouncingTop;
return isBouncing;
}
@end
9
我實現了擴展的UIScrollView來處理這種垂直和水平滾動。
Objective-C的
@interface UIScrollView (Bouncing)
@property (nonatomic, readonly) BOOL isBouncing;
@property (nonatomic, readonly) BOOL isBouncingTop;
@property (nonatomic, readonly) BOOL isBouncingLeft;
@property (nonatomic, readonly) BOOL isBouncingBottom;
@property (nonatomic, readonly) BOOL isBouncingRight;
@end
@implementation UIScrollView (Bouncing)
- (BOOL)isBouncing
{
return self.isBouncingTop || self.isBouncingLeft || self.isBouncingBottom || self.isBouncingRight;
}
- (BOOL)isBouncingTop
{
return self.contentOffset.y < - self.contentInset.top;
}
- (BOOL)isBouncingLeft
{
return self.contentOffset.x < - self.contentInset.left;
}
- (BOOL)isBouncingBottom
{
BOOL contentFillsScrollEdges = self.contentSize.height + self.contentInset.top + self.contentInset.bottom >= CGRectGetHeight(self.bounds);
return contentFillsScrollEdges && self.contentOffset.y > self.contentSize.height - CGRectGetHeight(self.bounds) + self.contentInset.bottom;
}
- (BOOL)isBouncingRight
{
BOOL contentFillsScrollEdges = self.contentSize.width + self.contentInset.left + self.contentInset.right >= CGRectGetWidth(self.bounds);
return contentFillsScrollEdges && self.contentOffset.x > self.contentSize.width - CGRectGetWidth(self.bounds) + self.contentInset.right;
}
@end
斯威夫特3.0+
extension UIScrollView {
var isBouncing: Bool {
return isBouncingTop || isBouncingLeft || isBouncingBottom || isBouncingRight
}
var isBouncingTop: Bool {
return contentOffset.y < -contentInset.top
}
var isBouncingLeft: Bool {
return contentOffset.x < -contentInset.left
}
var isBouncingBottom: Bool {
let contentFillsScrollEdges = contentSize.height + contentInset.top + contentInset.bottom >= bounds.height
return contentFillsScrollEdges && contentOffset.y > contentSize.height - bounds.height + contentInset.bottom
}
var isBouncingRight: Bool {
let contentFillsScrollEdges = contentSize.width + contentInset.left + contentInset.right >= bounds.width
return contentFillsScrollEdges && contentOffset.x > contentSize.width - bounds.width + contentInset.right
}
}
相關問題
- 1. 檢測地圖是否已滾動或不在地圖視圖
- 2. 如何在嵌套滾動視圖中禁用彈性滾動
- 3. 如何檢測JScrollPane的滾動條是否改變可視性?
- 4. 測試後彈簧測試不回滾
- 5. 嵌套滾動視圖內滾動視圖不起作用
- 6. 滾動視圖的子視圖如何知道滾動視圖正在滾動
- 7. 如何檢測滾動框(html元素)是否可滾動
- 8. 如何檢測用戶是否在UIScrollView中向後滾動了一個視圖?
- 9. 檢測UIScrollView是否滾動
- 10. 如何測試一個點是否在視圖中
- 11. 如何測試片段視圖對用戶是否可見?
- 12. tdd - 如何測試User.Identity.Name是否出現在剃刀視圖
- 13. 如何測試視圖是否位於頂層?
- 14. 如何測試Django響應是否由特定視圖生成?
- 15. Android:如何判斷一個視圖是否在iPhone中滾動
- 16. 如何檢查滾動視圖是否存在
- 17. 如何使用滾動視圖?是否有示例代碼?
- 18. 如何知道網格視圖是否滾動到結尾
- 19. Famo.us防止滾動視圖反彈
- 20. 滾動視圖是不可滾動
- 21. 如何與滾動視圖
- 22. android-如何把滾動視圖下滾動視圖
- 23. 檢測滾動視圖上的圖像
- 24. 如何檢測該視圖是動畫?
- 25. 如何同步滾動「背景滾動視圖」與「前景滾動視圖」
- 26. iOS如何滾動大父滾動視圖中的小孩子滾動視圖?
- 27. 如何使鍵盤與滾動視圖一起移動?
- 28. 滾動視圖滾動列表視圖不是佈局
- 29. 爲滾動視圖啓用滾動而不是列表視圖
- 30. 滾動視圖內滾動視圖android
:當內容不夠大覆蓋滾動視圖插圖這會更加有非零contentInsets和的情況下,非常感謝。我知道這種方式,我只是認爲還有一種方法。謝謝! :) – 2010-05-18 07:57:14