我在UIView中有一个UIImageView。在UIImageView内部,我在运行时添加了一个UIView(一个引脚),因此你可以多次添加相同的UIView并重新定位它。
问题是当UIImageView放大时UIView的重新定位。一旦UIImageView / UIScrollView放大,我无法抓住UIView(引脚)。整个UIImageView / UIScrollView移动但我可以&#39 ;抓住别针。当UIImageView缩小时,我仍然可以移动UIImageView,但我可以轻松选择并移动UIView。
关于如何在我放大时选择UIView而不是UIImageView的任何想法? 这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
imageView_image.image = originalImage;
scrollView.contentSize = self.view.frame.size;//originalImage.size;
scrollView.delegate = self;
scrollView.minimumZoomScale = 1.0;
scrollView.maximumZoomScale = 100.0;
}
- (IBAction)button_addAnnotation:(id)sender
{
annotationImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"push-pin-mini@3x.png"]];
[annotationImage setCenter:CGPointMake(imageView_image.bounds.size.width / 2, imageView_image.bounds.size.height / 2)];
annotationImage.userInteractionEnabled = YES;
imageView_image.userInteractionEnabled = YES;
panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
[annotationImage addGestureRecognizer:panRecognizer];
[imageView_image addSubview:annotationImage];
}
- (void)handlePan:(UIPanGestureRecognizer *)recognizer
{
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
if (recognizer.state == UIGestureRecognizerStateEnded)
{
CGPoint velocity = [recognizer velocityInView:self.view];
CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
CGFloat slideMult = magnitude / 200;
//NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult);
float slideFactor = 0.1 * slideMult; // Increase for more of a slide
CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor),
recognizer.view.center.y + (velocity.y * slideFactor));
finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width);
finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height);
[UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
recognizer.view.center = finalPoint;
} completion:nil];
}
}
感谢。
答案 0 :(得分:1)
如果您希望在尝试移动图钉时不滚动滚动视图,可以使用gestureRecognizer:shouldBeRequiredToFailByGestureRecognizer:
委托方法使滚动视图手势失败,例如:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
// Make the scroll view pan fail if the pin's "panRecognizer"
// is recognized
if (gestureRecognizer == scrollView.panGestureRecognizer &&
otherGestureRecognizer == panRecognizer) {
return YES;
}
return NO;
}