I'd like to increase and decrease font size with pinch in and out. but the following Gesture is very slow. how to optimize it to work as a UIWebView.
我想增加和减少字体大小随夹进和夹出。但是下面的手势非常慢。如何优化它作为一个UIWebView。
- (void)viewDidLoad {
textView.text = @"fdsakllllllllllllllllllllllllllllllfadsjksadfjkjkasdfjkjksfjkdajkasfdjkjhasdfhhjasdfhadfjkskasfdjkjkahsfdjkasdfjkjkasfdkjfsdahkafsdjkadjksfjkajksdfjkasdfkafjksdajksdffdsakllllllllllllllllllllllllllllllfadsjksadfjkjkasdfjkjksfjkdajkasfdjkjhasdfhhjasdfhadfjkskasfdjkjkahsfdjkasdfjkjkasfdkjfsdahkafsdjkadjksfjkajksdfjkasdfkafjksdajksdffdsakllllllllllllllllllllllllllllllfadsjksadfjkjkasdfjkjksfjkdajkasfdjkjhasdfhhjasdfhadfjkskasfdjkjkahsfdjkasdfjkjkasfdkjfsdahkafsdjkadjksfjkajksdfjkasdfkafjksdajksdffdsakllllllllllllllllllllllllllllllfadsjksadfjkjkasdfjkjksfjkdajkasfdjkjhasdfhhjasdfhadfjkskasfdjkjkahsfdjkasdfjkjkasfdkjfsdahkafsdjkadjksfjkajksdfjkasdfkafjksdajksfsdjkadjksfjkajksdfjkasdfkafjksdajksdffdsakllllllllllllllllllllllllllllllfadsjksadfjkjkasdfjkjksfjkdajkasfdjkjhasdfhhjasdfhadfjkskasfdjkjkahsfdjkasdfjkjkasfdkjfsdahkafsdjkadjksfjkajksdfjkasdfkafjksdajksdffdsakllllllllllllllllllllllllllllllfadsjksadfjkjkasdfjkjksfjkdajkasfdjkjhasdfhhjasdfhadfjkskasfdjkjkahsfdjkasdfjkjkasfdkjfsdahkafsdjkadjksfjkajksdfjkasdfkafjksdajksdffdsakllllllllllllllllllllllllllllllfadsjksadfjkjkasdfjkjksfjkdajkasfdjkjhasdfhhjasdfhadfjkskasfdjkjkahsfdjkasdfjkjkasfdkjfsdahkafsdjkadjksfjkajksdfjkasdfkafjksdajksdf";
UIPinchGestureRecognizer *pinchGesture =
[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];
pinchGesture.delegate = self;
/*textView.maximumZoomScale = 3;
textView.minimumZoomScale = .5;
*/
[textViewHadith addGestureRecognizer:pinchGesture];
[pinchGesture release];
// [textViewHadith setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"blackback ground.png"]]];
}
- (IBAction)handlePinchGesture:(UIGestureRecognizer *)sender {
// static CGRect initialBounds;
static int fontSize = 20;
// UIView *_view = sender.view;
if (sender.state == UIGestureRecognizerStateBegan)
{
// initialBounds = _view.bounds;
}
CGFloat factor = [(UIPinchGestureRecognizer *)sender scale];
// NSLog(@"factor = %f",factor);
if(factor > 1.0)
{
fontSize +=1;
}
else
{
fontSize -= 1;
}
// NSLog(@"font = %d",fontSize);
if (fontSize >50) {
fontSize =50; return;
}
if (fontSize <5) {
fontSize = 5; return;
}
[textView setFont:[UIFont fontWithName:@"Helvetica" size:fontSize]];
// CGAffineTransform zt = CGAffineTransformScale(CGAffineTransformIdentity, factor, factor);
// _view.bounds = CGRectApplyAffineTransform(initialBounds, zt);
return;
}
-(void) viewDidUnload
{
textView = nil;
}
@end
1 个解决方案
#1
3
Your code will increase or decrease the font size with every touch event, without regard to how much the fingers have moved. That will probably not give the users expected results.
您的代码将随着每次触摸事件而增加或减少字体大小,而不考虑手指移动了多少。这可能不会给用户带来预期的结果。
Instead you should record the font size when the gesture begins, and calculate the new font size relative to that based on the scale.
相反,您应该在手势开始时记录字体大小,并根据大小计算新的字体大小。
For example
例如
fontSize = startFontSize * factor;
Also, the code will run many times a second, and changing the font size is probably an expensive operation, because it must "reflow" the text.
此外,代码将每秒运行许多次,并且更改字体大小可能是一个昂贵的操作,因为它必须“回流”文本。
Instead, I would suggest a less expensive operation, just to give the users feedback that something is happening, but delaying the actual intended operation. Just set the transform on the view:
相反,我建议采用成本较低的操作,只是为了向用户反馈正在发生的事情,但要延迟实际预期的操作。只需在视图上设置转换:
view.transform = CGAffineTransformMakeScale(factor, factor);
You might also want to adjust the bounds (and set clipsToBounds) so that it appears the view keeps the same frame and that the position in the text under the gesture stays in frame and in the same position.
您可能还想调整边界(并设置clipsToBounds),使其显示视图保持相同的帧,并且手势下文本中的位置保持在帧和相同的位置。
Then at the end of the gesture, reset the transform to identity and modify the font size.
然后在手势结束时,将转换重置为标识并修改字体大小。
view.transform = CGAffineTransformIdentity;
fontSize = startFontSize * factor;
#1
3
Your code will increase or decrease the font size with every touch event, without regard to how much the fingers have moved. That will probably not give the users expected results.
您的代码将随着每次触摸事件而增加或减少字体大小,而不考虑手指移动了多少。这可能不会给用户带来预期的结果。
Instead you should record the font size when the gesture begins, and calculate the new font size relative to that based on the scale.
相反,您应该在手势开始时记录字体大小,并根据大小计算新的字体大小。
For example
例如
fontSize = startFontSize * factor;
Also, the code will run many times a second, and changing the font size is probably an expensive operation, because it must "reflow" the text.
此外,代码将每秒运行许多次,并且更改字体大小可能是一个昂贵的操作,因为它必须“回流”文本。
Instead, I would suggest a less expensive operation, just to give the users feedback that something is happening, but delaying the actual intended operation. Just set the transform on the view:
相反,我建议采用成本较低的操作,只是为了向用户反馈正在发生的事情,但要延迟实际预期的操作。只需在视图上设置转换:
view.transform = CGAffineTransformMakeScale(factor, factor);
You might also want to adjust the bounds (and set clipsToBounds) so that it appears the view keeps the same frame and that the position in the text under the gesture stays in frame and in the same position.
您可能还想调整边界(并设置clipsToBounds),使其显示视图保持相同的帧,并且手势下文本中的位置保持在帧和相同的位置。
Then at the end of the gesture, reset the transform to identity and modify the font size.
然后在手势结束时,将转换重置为标识并修改字体大小。
view.transform = CGAffineTransformIdentity;
fontSize = startFontSize * factor;