0
我已經通過編程方式不使用故事板創建了一個視圖內的圖像視圖。我爲該視圖添加了捏手勢識別器......但它應該以特定的比例以各種方式捏住圖像?有沒有辦法做到這一點?圖片+捏手勢識別器
-(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;
}
}
啊謝謝你開始工作,但現在有一個大問題..沒有開始和結束點的手勢..它變得太大或太小..我可以給具體的規模的手勢? –
我更新了我的問題,你現在可以檢查它嗎? –