我已经以编程方式在视图内部创建了一个imageview,而不是使用storyboard。我为该视图添加了捏手势识别器..但它应该以特定比例的各种方式捏图像?有没有办法做到这一点?
-(void)handlePinchWithGestureRecognizer:(UIPinchGestureRecognizer *)pinchGestureRecognizer{
CGFloat lastScale = 1.0;
if([pinchGestureRecognizer state] == UIGestureRecognizerStateBegan) {
// Reset the last scale, necessary if there are multiple objects with different scales
lastScale = [pinchGestureRecognizer scale];
}
if ([pinchGestureRecognizer state] == UIGestureRecognizerStateBegan ||
[pinchGestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGFloat currentScale = [[[pinchGestureRecognizer view].layer valueForKeyPath:@"transform.scale"] floatValue];
// Constants to adjust the max/min values of zoom
const CGFloat kMaxScale = 2.0;
const CGFloat kMinScale = 1.0;
CGFloat newScale = 1 - (lastScale - [pinchGestureRecognizer scale]);
newScale = MIN(newScale, kMaxScale / currentScale);
newScale = MAX(newScale, kMinScale / currentScale);
self.documentImageView.transform = CGAffineTransformScale(self.documentImageView.transform, newScale, newScale);
lastScale = [pinchGestureRecognizer scale]; // Store the previous scale factor for the next pinch gesture call
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchWithGestureRecognizer:)];
[self.documentImage addGestureRecognizer:pinchGestureRecognizer];
}
//
- (void)setExtractedImageForTableView:(UIImage *)extractedImage {
self.documentImageView.image = nil;
self.documentImageView = nil;
self.documentImageView = [[UIImageView alloc] initWithImage:extractedImage];
self.documentImageView.contentMode = UIViewContentModeScaleAspectFit;
if (!CGSizeEqualToSize(extractedImage.size, CGSizeZero)) {
self.documentImageView.frame = CGRectMake(CGRectGetMinX(self.view.frame), CGRectGetMinY(self.tableView.frame) + 70, CGRectGetWidth(self.view.frame), (extractedImage.size.height/extractedImage.size.width) * CGRectGetWidth(self.view.frame));
self.segmentedView.frame = self.documentImage.frame;
[self.segmentedView addSubview:self.segment];
[self.segmentedView addSubview:self.documentImageView];
[self.tableView.tableHeaderView addSubview:self.segment];
[self.tableView.tableHeaderView addSubview:self.documentImageView];
CGRect frame = self.documentImageView.frame;
frame.size.height = self.documentImageView.frame.size.height + 100;
self.tableView.frame = CGRectMake(0, 0, 400, 300);
self.tableView.tableHeaderView.frame = frame;
}
}
答案 0 :(得分:1)
我认为您错过了添加以下内容
[self.documentImageView setUserInteractionEnabled:YES];
希望这会奏效。