我自动生成一个NSURL,基于屏幕上一个pan手势的距离(说来话长……)。不管怎样,我没有问题生成URL。URL动态变化,然后我有一个NSTimer,它每秒钟重复一次,它告诉UIWebView用新的URL重新加载。
但是,尽管据报道URL正在更改,但UIWebView将只显示发送给它的第一个URL。我也在监控网站本身,它每秒钟都会收到第一个URL,而不是更新的URL。我的代码现在很简单,所以我不知道我做错了什么?任何帮助都将不胜感激。
在ViewController.h中:
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property float slicenumber;
@property NSURL *dynamicUrl;在ViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *fullURL = @"http://localhost:8080/remote/slicer/slice?view=Green&orientation=Axial&scrollTo=.50&size=native&fmt=png";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
[self.webView reload];
self.webView.scrollView.scrollEnabled = NO;
self.webView.scrollView.bounces = NO;
self.slicenumber=.5;
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(UpdateSlice) userInfo:nil repeats:YES];}
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
self.slicenumber=self.slicenumber+translation.y/350;
if (self.slicenumber<0) {
self.slicenumber=0;
}
if (self.slicenumber>1) {
self.slicenumber=1;
}
self.sliceloc.text= [NSString stringWithFormat:@"%.2f",self.slicenumber];
NSString *fullURL2 = [NSString stringWithFormat:@"http://localhost:8080/remote/slicer/slice?view=Green&orientation=Axial&scrollTo=%.2f&size=native&fmt=png",self.slicenumber];
self.dynamicUrl = [[NSURL alloc] initWithString:[fullURL2 stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
NSLog(@"urlString = %@",self.dynamicUrl);}
- (void)UpdateSlice {
[self.webView loadRequest:[NSURLRequest requestWithURL:self.dynamicUrl]];
[self.webView reload];}非常感谢!如果你需要其他信息,请告诉我。
发布于 2015-06-23 15:11:54
[self.webView reload] -将重新加载当前页面。这很可能是在loadRequest完成之前发生的。试着移除这一行。
此外,@joern的评论是正确的;'event‘是用户做了一个pan手势。把计时器丢了。
https://stackoverflow.com/questions/31006352
复制相似问题