2013-04-29 3 views
1

У меня есть UITextView внутри UIScrollView внутри UITextView. И я хочу добавить «умение масштабирования» в UITextView.UITextView в UIScrollView в UIView: иерархия IBOutlet?

Я добавил < ... UIScrollViewDelegate> в формате .h. Я добавил это в файл .m.

@synthesize myTextView; 
@synthesize scrollView; 

- (void)scrollViewDidZoom:(UIScrollView *)sv 
{ 
    float zoomScale = sv.zoomScale; 
    if (zoomScale < 3) 
    { 
     if(zoomScale < 0.5) 
     { 
      UIFont* myFont = [UIFont systemFontOfSize:12]; 
      myTextView.font = myFont; 
     }else{ 
      UIFont* myFont = [UIFont systemFontOfSize:(zoomScale * 12)]; 
      myTextView.font = myFont; 
     } 
     UIFont* myFont = [UIFont systemFontOfSize:36]; 
     myTextView.font = myFont; 
    } 
} 

"scrollViewDidZoom" никогда не называется.

Я успешно добавляю эти объекты с помощью построителя интерфейса. Честно говоря, я немного потерян. Я не знаю, как я должен создавать выходы и делегаты (с 3 уровня иерархии).

ответ

1

Я понял, что есть простой способ сделать это с помощью UIPinchGestureRecognizer.

- (void)viewDidLoad 
{ 
    //... 
    UIPinchGestureRecognizer *pinchGest = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(changeTextViewFontSize:)]; 
    pinchGest.delegate = self; 
    [myTextView addGestureRecognizer:pinchGest]; 
    [pinchGest release]; 
} 

- (void)changeTextViewFontSize:(UIPinchGestureRecognizer *)p 
{ 
    CGFloat zoomVelocity = [(UIPinchGestureRecognizer *)p velocity]; 
    UIFont *font = self.myTextView.font; 
    CGFloat pointSize = font.pointSize; 
    NSString *fontName = font.fontName; 

    pointSize = ((zoomVelocity > 0) ? 1 : -1) * 1 + pointSize; 

    if (pointSize < 8) pointSize = 8; 
    if (pointSize > 32) pointSize = 32; 

    self.myTextView.font = [UIFont fontWithName:fontName size:pointSize]; 
} 

Спасибо Aral Balkan: http://aralbalkan.com/3831/

Смежные вопросы