3

这是写在我的 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;
    }
}
4

2 回答 2

0

您请将您的 imageView 的 UserInteractionsenable 设置为 YES,因为默认情况下它设置为 NO。然后它将正常工作。

于 2015-02-19T04:04:04.837 回答
0

不确定天气我是否正确,但从您的代码如下:

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

您正在尝试在 iVar 上添加手势,但将局部变量添加到 self.contentView。

于 2014-03-06T12:01:12.180 回答