gpt4 book ai didi

ios - 未调用 UIScrollViewDelegate 方法

转载 作者:行者123 更新时间:2023-11-29 02:46:00 26 4
gpt4 key购买 nike

我已经实现了这些方法,将 View Controller 指定为 UIScrollViewDelegate,设置内容大小,并设置 UIScrollView 的委托(delegate)属性。但是,出于某种原因,没有调用任何委托(delegate)方法。

该应用程序在 UIScrollView 中有一个 UITextView,我希望能够始终知道出现的文本的范围,从而知道我在文档中的位置。下面的代码是一个证明这个概念的测试应用程序。我可以触发我的方法来查找显示的文本,但只能在开始时触发,而我想在每次滚动完成时触发它以确保结果始终正确。

该应用程序设置为单 View 应用程序。完整的程序如下。任何帮助将不胜感激!

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIScrollViewDelegate> {
UITextView *lTextView;
}
@end

ViewController.m #import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

int statusBarHeight = 20;

UIScrollView *scrollView;
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, statusBarHeight, self.view.frame.size.width, self.view.frame.size.height - statusBarHeight)];
[self.view addSubview:scrollView];

lTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.height)];
lTextView.editable = NO;
lTextView.text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam congue eleifend risus eget pretium. Donec sed commodo neque, id ornare dolor. Vivamus vestibulum non quam et euismod. Morbi et dolor luctus velit lobortis ornare vel vel tellus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aenean viverra, urna nec tempor commodo, turpis nisl rhoncus mauris, in ullamcorper justo sapien quis nulla. Aliquam at odio molestie, laoreet elit sed, suscipit risus. Nulla eleifend, quam eget porttitor condimentum, metus lacus lobortis ligula, accumsan tristique neque turpis non purus. Aenean malesuada tortor id elit semper, et pretium nulla viverra. Aliquam sollicitudin placerat massa, quis posuere est ornare vel. Nam mollis convallis risus a tincidunt. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Fusce facilisis orci nisi, vel egestas metus tristique vitae. Proin nec malesuada dolor. Donec eget felis accumsan, facilisis turpis vitae, blandit lacus. Pellentesque auctor nisl quis turpis commodo lacinia. In sed euismod urna. Praesent sed commodo magna. Ut interdum dignissim urna, nec feugiat dolor. Nulla facilisi. Donec fermentum mauris at ante tincidunt, id accumsan eros lacinia. Suspendisse potenti. Integer ac mattis eros, sed volutpat dui. Pellentesque vehicula turpis ut metus malesuada blandit. Nam laoreet dui id imperdiet pulvinar. In auctor enim ac massa feugiat adipiscing. Nam convallis neque at felis tincidunt iaculis. Maecenas dictum est ac nulla suscipit, nec condimentum metus molestie. Vestibulum mollis velit eu nunc eleifend egestas. Ut aliquam ultrices tellus volutpat consectetur. Morbi eget sollicitudin quam, ut imperdiet leo. Morbi sed ligula iaculis, tincidunt diam nec, pharetra ligula. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Proin varius facilisis placerat. Fusce suscipit risus risus, in varius diam convallis quis. In hac habitasse platea dictumst. Integer non lectus non dolor fringilla venenatis eget quis nisl. Proin in pretium metus. Pellentesque sed tellus iaculis, bibendum neque vel, porta ante. Phasellus eu vulputate massa. Nullam venenatis lectus non nunc aliquet porta. Nunc gravida rutrum feugiat. Fusce elit nunc, facilisis non tristique placerat, tempor id orci. Mauris et massa cursus, dapibus urna a, condimentum arcu. Pellentesque vitae sagittis sapien. Ut lacus purus, suscipit at magna non, rhoncus luctus dui. Ut ipsum augue, pharetra ac ipsum sed, facilisis convallis justo. Ut arcu augue, hendrerit vel tincidunt vitae, aliquet ac quam. Nulla ullamcorper, dolor eu pellentesque cursus, lectus quam interdum ante, nec congue dui augue nec dolor. Sed convallis elit in enim dictum, at posuere sem mollis. Praesent in metus aliquam, ullamcorper purus tempor, mattis ipsum. Aliquam gravida, sem vitae iaculis placerat, dui velit commodo nulla, vitae ultrices lectus dolor ut mi. Suspendisse quis metus varius, congue turpis vitae, viverra nunc. Duis placerat, felis et laoreet pretium, nibh lorem pulvinar turpis, eu euismod arcu libero at mauris. Sed laoreet, eros in tempor accumsan, odio augue fermentum dui, a pharetra felis libero eu ligula. Suspendisse ultricies pulvinar urna. Donec placerat nulla non elit vestibulum mattis. Phasellus semper sem a sem dignissim tempus. Sed scelerisque sed purus interdum rhoncus. Sed gravida eros sit amet dui fermentum rutrum. Proin a arcu scelerisque, volutpat orci quis, dignissim neque. Aliquam erat volutpat. Sed eget scelerisque neque.";

[scrollView addSubview:lTextView];
[scrollView setContentSize:CGSizeMake(self.view.frame.size.width, 2000)];
scrollView.delegate = self;

[self report];
}

- (void)report {
NSArray *visibleRange = [self visibleRangeOfTextView:lTextView];
NSNumber *start = [visibleRange objectAtIndex:0];
NSNumber *end = [visibleRange objectAtIndex:1];
int rangeLength = (end.intValue - start.intValue);

NSLog(@"%@", [lTextView.text substringWithRange:NSMakeRange(start.intValue, rangeLength)]);

}


-(NSArray *)visibleRangeOfTextView:(UITextView *)textView {
CGRect bounds = textView.bounds;

UITextPosition *start = [textView characterRangeAtPoint:bounds.origin].start;
UITextPosition *end = [textView characterRangeAtPoint:CGPointMake(CGRectGetMaxX(bounds), CGRectGetMaxY(bounds))].end;

float startOffset = [textView offsetFromPosition:textView.beginningOfDocument toPosition:start];
float endOffset = [textView offsetFromPosition:textView.beginningOfDocument toPosition:end];

return [NSArray arrayWithObjects:[NSNumber numberWithFloat:startOffset], [NSNumber numberWithFloat:endOffset], nil];

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSLog(@"scrollViewDidEndDecelerating");
[self report];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
NSLog(@"scrollViewWillBeginDragging");
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
NSLog(@"scrollViewDidEndScrollingAnimation");
}
@end

最佳答案

我 99% 确定您的 ScrollView 没有滚动,实际上是您的 UITextView 正在执行所有滚动。如果这是真的,您可以将自己设为 UITextView 的 UIScrollViewDelegate,因为 UITextView 是 UIScrollView 的子类。

关于ios - 未调用 UIScrollViewDelegate 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25123215/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com