如何优化UIPinchGestureRecognizer来增加和减少字体大小?

时间:2011-10-30 14:27:17

标签: iphone

我想通过捏合来增加和减少字体大小。但是下面的手势很慢。如何优化它作为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 个答案:

答案 0 :(得分:3)

您的代码会在每次触摸事件时增加或减少字体大小,而不考虑手指移动了多少。这可能不会给用户带来预期的结果。

相反,您应该在手势开始时记录字体大小,并根据比例计算新字体大小。

例如

fontSize = startFontSize * factor;

此外,代码将每秒运行多次,并且更改字体大小可能是一项昂贵的操作,因为它必须“重排”文本。

相反,我会建议一个更便宜的操作,只是为了向用户提供有关正在发生的事情的反馈,但是延迟了实际的预期操作。只需在视图上设置转换:

view.transform = CGAffineTransformMakeScale(factor, factor);

您可能还需要调整边界(并设置clipsToBounds),以使视图保持相同的框架,并且手势下文本中的位置保持在框架中且处于相同位置。

然后在手势的 end 处,将转换重置为identity并修改字体大小。

view.transform = CGAffineTransformIdentity;
fontSize = startFontSize * factor;