2011-10-30 164 views
3

我想增加和縮小字體大小與捏和進出。但是下面的手勢非常緩慢。如何優化它作爲UIWebView工作。如何優化UIPinchGestureRecognizer增加和減小字體大小?

- (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 

回答

3

您的代碼將增加或減少每個觸摸事件的字體大小,而不考慮手指的移動量。這可能不會給用戶預期的結果。

相反,您應該在手勢開始時記錄字體大小,並計算相對於基於比例的字體大小。

例如

fontSize = startFontSize * factor; 

此外,該代碼將運行很多次第二,並改變字體大小可能是一個昂貴的操作,因爲它必須「迴流」的文字。相反,我會建議一個更便宜的操作,只是爲了讓用戶反饋某些事情正在發生,但是延遲了實際的預期操作。只設置視圖的變換:

view.transform = CGAffineTransformMakeScale(factor, factor); 

您可能還需要調整邊界(並設置clipsToBounds),以便它出現的觀點保持相同的框架和姿態下的文本的位置停留在框架和相同的位置。

然後在手勢的結束處,重置變換以標識和修改字體大小。

view.transform = CGAffineTransformIdentity; 
fontSize = startFontSize * factor; 
相關問題