2013-04-25 3 views
3

이것은 내 UICollectionViewCell로 작성되었지만 핀치가 호출되지 않습니다.사용자 정의 UICollectionViewCell 확대/축소 이미지

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     [self.contentView setBackgroundColor:[UIColor clearColor]]; 

     UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0, 0.0, frame.size.width,frame.size.height)]; 
     UIPinchGestureRecognizer *pgr = [[UIPinchGestureRecognizer alloc] 
             initWithTarget:self action:@selector(pinch:)]; 
     [self.imageView addGestureRecognizer:pgr]; 
     [self.contentView addSubview:imageView]; 
     imageView.contentMode=UIViewContentModeScaleAspectFill; 
     [imageView setClipsToBounds:TRUE]; 

     _imageView = imageView; 

     UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; 
     activityIndicator.center = _imageView.center; 
     activityIndicator.hidesWhenStopped = YES; 
     [_imageView addSubview:activityIndicator]; 
     _activityIndicator=activityIndicator; 

     self.imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; 


    } 
    return self; 
} 
- (void)pinch:(UIPinchGestureRecognizer *)gesture { 
    if (gesture.state == UIGestureRecognizerStateEnded 
     || gesture.state == UIGestureRecognizerStateChanged) { 
     NSLog(@"gesture.scale = %f", gesture.scale); 

     CGFloat currentScale = self.frame.size.width/self.bounds.size.width; 
     CGFloat newScale = currentScale * gesture.scale; 

     if (newScale < 1.0) { 
      newScale = 1.0; 
     } 
     if (newScale > 4.0) { 
      newScale = 4.0; 
     } 

     CGAffineTransform transform = CGAffineTransformMakeScale(newScale, newScale); 
     self.transform = transform; 
     gesture.scale = 1; 
    } 
} 

답변

0

는 확실하지 날씨 나는 올바른하지만 아래 코드에서 오전 : 당신은 바르의 제스처를 추가하려고하지만 self.contentView에 지역 변수를 추가

 [self.imageView addGestureRecognizer:pgr]; 
     [self.contentView addSubview:imageView]; 

.

0

당신이 ImageView에 대해 UserInteractionsenable을 YES로 설정하십시오. 기본값으로 NO로 설정되어 있기 때문에, 정상적으로 작동합니다.

관련 문제