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.
- (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
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
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:
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.
Then at the end of the gesture, reset the transform to identity and modify the font size.