【发布时间】:2015-06-23 14:58:49
【问题描述】:
我会根据屏幕上平移手势的距离自动生成一个 NSURL(长篇大论……)。无论如何,我在生成 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];}
非常感谢!如果您需要任何其他信息,请告诉我。
【问题讨论】:
-
不能解决你的问题,但一般来说:你不应该为此使用计时器。只需从“handlePan:”调用“updateSlices”(并且仅在切片编号更改时)
标签: ios objective-c xcode uiwebview