这是在我的UICollectionViewCell中写的,但是pinch永远不会被调用。
- (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 :(得分:0)
不确定天气我是否正确,但请使用下面的代码:
[self.imageView addGestureRecognizer:pgr];
[self.contentView addSubview:imageView];
您正尝试在iVar上添加手势,但在self.contentView中添加了一个局部变量。
答案 1 :(得分:0)
请为您的imageView设置UserInteractionsenable为YES,因为默认情况下它设置为NO。然后它将正常工作。