1

我正在使用此代码IBAction来创建一个UIImageView带有触摸拖动的内部 UIButton

-(IBAction) addNewPhoto:(id)sender{

switch ([sender tag]) {
    case 0:{

        imageMove = [UIImage imageNamed:@"knet.jpg"];

         holderView = [[UIView alloc] initWithFrame:CGRectMake(0,0, imageMove.size.width, imageMove.size.height)];
        UIImageView *imageview = [[UIImageView alloc] initWithFrame:[holderView frame]];
        [imageview setImage:imageMove];
        [holderView addSubview:imageview];

        UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
        [pinchRecognizer setDelegate:self];
        [holderView addGestureRecognizer:pinchRecognizer];

        UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
        [rotationRecognizer setDelegate:self];
        [holderView addGestureRecognizer:rotationRecognizer];

        UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
        [panRecognizer setMinimumNumberOfTouches:1];
        [panRecognizer setMaximumNumberOfTouches:1];
        [panRecognizer setDelegate:self];
        [holderView addGestureRecognizer:panRecognizer];

        UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
        [tapRecognizer setNumberOfTapsRequired:1];
        [tapRecognizer setDelegate:self];
        [holderView addGestureRecognizer:tapRecognizer];

        [self.view addSubview:holderView];

        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:.5];
        [superView setFrame:CGRectMake(0, 0, 320, 550)];
        //[holderView setFrame:CGRectMake(0, 0, 320, 550)];
        //holderView.alpha = 1;
        [UIView commitAnimations];

    }
        break;
    case 1:
    {
        imageMove = [UIImage imageNamed:@"test.jpg"];

        //..........similar to case 0 but another picture
         }
        break;

    default:
        break;
}

按钮在 a 内scrollView,我正在寻找的是,应该在用户将他/她的手指从按钮上拖下来并且仍然能够在不移开他/她的手指的情况下移动它的地方创建图像。

我使用源代码来移动、缩放、旋转这个博客中的图像,并修改了使用按钮而不是 UIImagePickerController 创建图像。

4

2 回答 2

1

你可以使用 a UILongPressGestureRecognizer。如果添加UILongPressGestureRecognizer到主视图(holderView的超级视图)中,可以检测被UILongPressGestureRecognizer调用选择器中的状态来决定做什么。如果您在识别手势后移动手指,则具有UILongPressGestureRecognizer以下状态:UIGestureRecognizerStateChanged

- (void)createNewImage:(UILongPressGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
        // create a new image, add to the view, and position at the location of the touch
        // store a reference to it in an ivar
    } else if (([gestureRecognizer state] == UIGestureRecognizerStateChanged)) {
        // move the image stored in the ivar
    } else {
        // set the ivar to nil
        // add the standard gesture recognisers to the new image
    }
}

为了进一步解释,向视图控制器添加一个新变量:

@interface ViewController
{
    UIView *_newView;
}
@end

然后将其添加到您的视图控制器的viewDidLoad方法中:

UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(createNewImage:)];
[self.view addGestureRecognizer:longPressGesture];

然后将方法添加createNewImage到视图控制器:

- (void)createNewImage:(UILongPressGestureRecognizer *)gestureRecognizer
{
    CGPoint translation = [gestureRecognizer locationInView:self.view];

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) { //press has begun    
        UIImage *image = [UIImage imageNamed:@"knet.jpg"];
        UIView *holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
        UIImageView *imageview = [[UIImageView alloc] initWithFrame:[holderView frame]];
        [imageview setImage:image];
        [holderView addSubview:imageview];

        [holderView setCenter:CGPointMake(translation.x, translation.y)];
        _newView = holderView;

        [self.view addSubview:holderView];
    } else if (([gestureRecognizer state] == UIGestureRecognizerStateChanged)) { //finger is moving

        [_newView setCenter:CGPointMake(translation.x, translation.y)];

    } else { //finger lifted off to end the long press

        // here, you'll have to add all the other gesture recognisers to 
        // _newView in case if the new image is moved
        _newView = nil;
    }
}

附带一点:我保留了您的holderView视图层次结构,但它似乎是多余的,您可以UIImageView在没有持有人视图的情况下拖动一个周围。

希望有帮助

于 2013-01-07T09:18:35.647 回答
0

我建议使用 Ray Wenderlich 的这篇文章。它提供了您想要的一切,拖动,这显然是您要寻找的,它提供了一种用手指拖动图像的解决方案,甚至为其添加了很酷的速度效果!捏缩放,触摸,旋转。您可以从那里下载示例代码,这有助于我掌握手势识别器。

罗汉

于 2013-01-07T09:16:59.033 回答