如何在相同的图像视图上实现不同的GestureRecognizer?

发布于 2024-11-04 22:47:59 字数 1846 浏览 0 评论 0原文

我正在我的应用程序中实现旋转、缩放、点击手势识别器。我有一个图像视图,在其中获取用户图像,然后有按钮移动到图章视图,其中有 120 个 1000*100 的可滚动图章图像。问题是,当我选择一个图章图像时,手势工作正常。但是当我再次移动到图章视图并选择图章时,第一个图章变为静态并且不识别任何手势,只有当前图章识别该手势。 我正在执行的是选择多个图章,然后我可以旋转它们,拉伸它们,捏它们。 这是我正在实现的代码。请帮助我如何实现这一目标...

-(void)viewWillAppear:(BOOL)animated
{
if (stampImageView) {
        [stampImageView release];
    }
    stampImageView=[[UIImageView alloc]initWithFrame:CGRectMake(self.view.center.x-100, 200, 80, 80)];
    stampImageView.tag=(int)mAppDel.frameImageString;
    NSLog(@"tag is %@",stampImageView.tag);
    stampImageView.userInteractionEnabled=YES;
    if(mAppDel.frameImageString)
    stampImageView.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:mAppDel.frameImageString ofType:@"png"]];
    [self.view addSubview:stampImageView];
    stampImageView.userInteractionEnabled=YES;
                [self.view bringSubviewToFront:stampImageView];
                UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotatePiece:)];

                [stampImageView addGestureRecognizer:rotationGesture];
                [rotationGesture release];

                UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scalePiece:)];
                [pinchGesture setDelegate:self];
                [stampImageView addGestureRecognizer:pinchGesture];
                [pinchGesture release];

                UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panPiece:)];
                [panGesture setMaximumNumberOfTouches:1];
                [panGesture setDelegate:self];
                [stampImageView addGestureRecognizer:panGesture];
                [panGesture release];

}

I am implementing the rotation,pich,tap gesture recognizers in my app.I have an image view where i get the user image,then there is button to move to the stamps view,where there are 120 scrollable stamp images of 1000*100.The problem is that when i choose one stampimage,the gesture works fine.But when i again move to the stamp view and choose stamp,the first one stamp become static and does not recognize any gesture,only the current stamp recognize the gesture.
What i am performing is to select multiple stamps and then i can rotate them,strech them,pinch them.
Here is the code which i am implementing.Just help me how to acheive this...

-(void)viewWillAppear:(BOOL)animated
{
if (stampImageView) {
        [stampImageView release];
    }
    stampImageView=[[UIImageView alloc]initWithFrame:CGRectMake(self.view.center.x-100, 200, 80, 80)];
    stampImageView.tag=(int)mAppDel.frameImageString;
    NSLog(@"tag is %@",stampImageView.tag);
    stampImageView.userInteractionEnabled=YES;
    if(mAppDel.frameImageString)
    stampImageView.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:mAppDel.frameImageString ofType:@"png"]];
    [self.view addSubview:stampImageView];
    stampImageView.userInteractionEnabled=YES;
                [self.view bringSubviewToFront:stampImageView];
                UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotatePiece:)];

                [stampImageView addGestureRecognizer:rotationGesture];
                [rotationGesture release];

                UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scalePiece:)];
                [pinchGesture setDelegate:self];
                [stampImageView addGestureRecognizer:pinchGesture];
                [pinchGesture release];

                UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panPiece:)];
                [panGesture setMaximumNumberOfTouches:1];
                [panGesture setDelegate:self];
                [stampImageView addGestureRecognizer:panGesture];
                [panGesture release];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余生共白头 2024-11-11 22:47:59

你尝试过使用委托方法吗

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

Did u try using the delegate method

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
复古式 2024-11-11 22:47:59

我为单点触控实现了这个逻辑,但你可以在你的应用程序中使用它

void AddGestureRecognizersToImage (UIImageView imgView)
{
    imgView.UserInteractionEnabled = true;

    // rotate the images
    var rotationGesture = new UIRotationGestureRecognizer (this, new Selector ("RotateImage"));
    imgView.AddGestureRecognizer (rotationGesture);

    // Zoom the image
    var pinchGesture = new UIPinchGestureRecognizer (this, new Selector ("ScaleImage"));
    //pinchGesture.Enabled = true;
    pinchGesture.Delegate = new GestureDelegate (this);
    imgView.AddGestureRecognizer (pinchGesture);

    var panGesture = new UIPanGestureRecognizer(this, new Selector ("PanImage"));
    //panGesture.Enabled = true;
    panGesture.MaximumNumberOfTouches = 2;
    panGesture.Delegate = new GestureDelegate (this);
    imgView.AddGestureRecognizer (panGesture);

    var longPressGesture = new UILongPressGestureRecognizer (this, new Selector ("ShowResetMenu"));
    imgView.AddGestureRecognizer (longPressGesture);
}

void AdjustAnchorPointForGestureRecognizer (UIGestureRecognizer gestureRecognizer)
{
    if (gestureRecognizer.State == UIGestureRecognizerState.Began)
    {
        var image = gestureRecognizer.View;
        var locationInView = gestureRecognizer.LocationInView (image);
        var locationInSuperview = gestureRecognizer.LocationInView (image.Superview);

        image.Layer.AnchorPoint = new PointF (locationInView.X / image.Bounds.Size.Width, locationInView.Y / image.Bounds.Size.Height);
        image.Center = locationInSuperview;
    }
}

[Export("RotateImage")]
void RotateImage (UIRotationGestureRecognizer gestureRecognizer)
{
    AdjustAnchorPointForGestureRecognizer (gestureRecognizer);
    if (gestureRecognizer.State == UIGestureRecognizerState.Began || gestureRecognizer.State == UIGestureRecognizerState.Changed)
    {
        gestureRecognizer.View.Transform *= CGAffineTransform.MakeRotation (gestureRecognizer.Rotation);

        // Reset the gesture recognizer's rotation - the next callback will get a delta from the current rotation.
        gestureRecognizer.Rotation = 0;
    }
}

// Zoom the image by the current scale

[Export("ScaleImage")]
void ScaleImage (UIPinchGestureRecognizer gestureRecognizer)
{
    AdjustAnchorPointForGestureRecognizer (gestureRecognizer);
    if (gestureRecognizer.State == UIGestureRecognizerState.Began || gestureRecognizer.State == UIGestureRecognizerState.Changed)
    {
        gestureRecognizer.View.Transform *= CGAffineTransform.MakeScale (gestureRecognizer.Scale, gestureRecognizer.Scale);
        // Reset the gesture recognizer's scale - the next callback will get a delta from the current scale.
        gestureRecognizer.Scale = 1;
    }
}

// Shift the image's center by the pan amount

[Export("PanImage")]
void PanImage (UIPanGestureRecognizer gestureRecognizer)
{           
    gestureRecognizer.Enabled = true;
    AdjustAnchorPointForGestureRecognizer (gestureRecognizer);

    var image = gestureRecognizer.View;

    if (gestureRecognizer.State == UIGestureRecognizerState.Began || gestureRecognizer.State == UIGestureRecognizerState.Changed)
    {           
        var translation = gestureRecognizer.TranslationInView (this.window);
        gestureRecognizer.View.Center = new PointF (gestureRecognizer.View.Center.X + translation.X, gestureRecognizer.View.Center.Y + translation.Y);

        //image.Center = new PointF (image.Center.X + translation.X, image.Center.Y + translation.Y);

        // Reset the gesture recognizer's translation to {0, 0} - the next callback will get a delta from the current position.
        gestureRecognizer.SetTranslation (PointF.Empty, image);
    }
}

I implement this logic for monotouch but you can use it in you app

void AddGestureRecognizersToImage (UIImageView imgView)
{
    imgView.UserInteractionEnabled = true;

    // rotate the images
    var rotationGesture = new UIRotationGestureRecognizer (this, new Selector ("RotateImage"));
    imgView.AddGestureRecognizer (rotationGesture);

    // Zoom the image
    var pinchGesture = new UIPinchGestureRecognizer (this, new Selector ("ScaleImage"));
    //pinchGesture.Enabled = true;
    pinchGesture.Delegate = new GestureDelegate (this);
    imgView.AddGestureRecognizer (pinchGesture);

    var panGesture = new UIPanGestureRecognizer(this, new Selector ("PanImage"));
    //panGesture.Enabled = true;
    panGesture.MaximumNumberOfTouches = 2;
    panGesture.Delegate = new GestureDelegate (this);
    imgView.AddGestureRecognizer (panGesture);

    var longPressGesture = new UILongPressGestureRecognizer (this, new Selector ("ShowResetMenu"));
    imgView.AddGestureRecognizer (longPressGesture);
}

void AdjustAnchorPointForGestureRecognizer (UIGestureRecognizer gestureRecognizer)
{
    if (gestureRecognizer.State == UIGestureRecognizerState.Began)
    {
        var image = gestureRecognizer.View;
        var locationInView = gestureRecognizer.LocationInView (image);
        var locationInSuperview = gestureRecognizer.LocationInView (image.Superview);

        image.Layer.AnchorPoint = new PointF (locationInView.X / image.Bounds.Size.Width, locationInView.Y / image.Bounds.Size.Height);
        image.Center = locationInSuperview;
    }
}

[Export("RotateImage")]
void RotateImage (UIRotationGestureRecognizer gestureRecognizer)
{
    AdjustAnchorPointForGestureRecognizer (gestureRecognizer);
    if (gestureRecognizer.State == UIGestureRecognizerState.Began || gestureRecognizer.State == UIGestureRecognizerState.Changed)
    {
        gestureRecognizer.View.Transform *= CGAffineTransform.MakeRotation (gestureRecognizer.Rotation);

        // Reset the gesture recognizer's rotation - the next callback will get a delta from the current rotation.
        gestureRecognizer.Rotation = 0;
    }
}

// Zoom the image by the current scale

[Export("ScaleImage")]
void ScaleImage (UIPinchGestureRecognizer gestureRecognizer)
{
    AdjustAnchorPointForGestureRecognizer (gestureRecognizer);
    if (gestureRecognizer.State == UIGestureRecognizerState.Began || gestureRecognizer.State == UIGestureRecognizerState.Changed)
    {
        gestureRecognizer.View.Transform *= CGAffineTransform.MakeScale (gestureRecognizer.Scale, gestureRecognizer.Scale);
        // Reset the gesture recognizer's scale - the next callback will get a delta from the current scale.
        gestureRecognizer.Scale = 1;
    }
}

// Shift the image's center by the pan amount

[Export("PanImage")]
void PanImage (UIPanGestureRecognizer gestureRecognizer)
{           
    gestureRecognizer.Enabled = true;
    AdjustAnchorPointForGestureRecognizer (gestureRecognizer);

    var image = gestureRecognizer.View;

    if (gestureRecognizer.State == UIGestureRecognizerState.Began || gestureRecognizer.State == UIGestureRecognizerState.Changed)
    {           
        var translation = gestureRecognizer.TranslationInView (this.window);
        gestureRecognizer.View.Center = new PointF (gestureRecognizer.View.Center.X + translation.X, gestureRecognizer.View.Center.Y + translation.Y);

        //image.Center = new PointF (image.Center.X + translation.X, image.Center.Y + translation.Y);

        // Reset the gesture recognizer's translation to {0, 0} - the next callback will get a delta from the current position.
        gestureRecognizer.SetTranslation (PointF.Empty, image);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文