Comment puis-je tester si la vue de défilement rebondit? Y at-il une notification ou quelque chose quand le rebond se termine?Comment puis-je tester si la vue de défilement rebondit?
Répondre
Oui ... vérifier la spécification UIScrollViewDelegate, mettre en œuvre les méthodes, y compris les deux ci-dessous, et définissez votre délégué UIScrollView en conséquence:
// 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;
Vous serez probablement plus intéressé par scrollViewDidEndDecelerating. Ceux-ci fonctionnent également dans UITableView, où je les ai trouvés à l'origine (UITableView hérite de UIScrollView).
Voici comment je détecté si la vue de défilement est rebondissant horizontalement:
- (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");
}
}
Une modification mineure à la méthode de Justin, ce qui permet 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");
}
Pour ceux d'entre vous qui seraient en mesure "rebondir" vers le bas dans un scrollview afin de mettre à jour le contenu de la vue. (Et l'événement est tiré seulement une fois.)
- (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");
}
}
question ancienne, mais je viens de croiser un problème similaire et je voulais ajouter que c'est une bonne idée de vérifier que le contenu de vues de défilement est plus grand que le défilement le cadre de vue:
+ (BOOL) isScrollViewBouncing:(UIScrollView *)scrollView
{
return scrollView.contentOffset.y > scrollView.contentSize.height - scrollView.frame.size.height
&& scrollView.contentSize.height > scrollView.frame.size.height;
}
Cela fait maintenant que la vue de défilement est assez grand pour être dans un état de défilement rebondissant, de sorte que si la vue de défilement est petit, il ne signifie pas toujours évaluer true.
Vive
En utilisant la réponse de Glavid, j'ai ajouté rebondir en bas aussi, et ajouté en tant que catégorie
#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
J'ai l'extension pour UIScrollView à mis en œuvre le font à défilement vertical et horizontal. Cela fonctionne même avec contentInsets non nuls et dans le cas où le contenu est pas assez grand pour couvrir ScrollView: empiècements
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
Swift 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
}
}
Merci beaucoup. Je sais de cette façon, je pensais juste qu'il existe une autre méthode. Merci! :) –