简体   繁体   中英

iOS Scrollview detect Scrolling

I have read all the previous posts about this exact topic, but I still cannot detect when the user is scrolling on this iPad app...

My .h:

#import <UIKit/UIKit.h>

@interface MyApp_ViewController : UIViewController <UIScrollViewDelegate>
{

}
@property (weak, nonatomic) IBOutlet UIButton *otherButton;
@property (weak, nonatomic) IBOutlet UIScrollView *otherScrollView;
@property (weak, nonatomic) IBOutlet UITextView *txtViewHTML;

-(void)k_RootViewPrint:(NSString *) data;

-(void)ActionEventForButton: (id) sender;

@end

my .m:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView 
{

    NSLog(@"...");
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"2222");
}
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event 
{
    NSLog(@"touches ended BS");
}

But none of them work? I'm sure it's something simple, but I've done everything I can think of and still nothing. Any guidance in the correct direction would be deeply appreciated!

If it's not logging in scrollViewDidScroll: , then the scroll view's delegate is not set to the view controller.

You can check this in the debugger. After your app has launched, pause in the debugger and run this command:

po [[(id)UIApp keyWindow] recursiveDescription]

Look through the output to find the address of your UIScrollView. There will be a line that says something like <UIScrollView: 0xc894d60 ...> . Take the address ( 0xc894d60 in my example) and use it in this debugger command:

po [0xc894d60 delegate]

If it prints something like <MyApp_ViewController: 0x6a7f6c0> , your delegate is set correctly. If it prints "Can't print the description of a NIL object.", you have not set the scroll view's delegate. If it prints something else, you have set the scroll view's delegate incorrectly.

您是否将UIScrollView上的委托设置为ViewController?

For others who might be lost like I was, here's the solution in code:

- (void)viewDidLoad
{
[super viewDidLoad];
self.otherScrollView.delegate = self; // This is the KEY

Check whether you have set the delegate or not and then try

Try implementing `

- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event 
{   
    // If not dragging, send event to next responder
    if (!self.dragging) 
        [self.nextResponder touchesEnded: touches withEvent:event]; 
    else
        [super touchesEnded: touches withEvent: event];
}
`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM