pan识别器和滑块之间存在[C4]冲突

时间:2013-10-17 13:44:22

标签: ios objective-c c4

我正在尝试平移和放大图像。由于C4中还没有pan-gesture,我认为我可以在开始时使用滑块。 所以我将pan-gesture添加到照片中,如下所示:

-(void)photoToCrop{
    photoTaken=[C4Image imageNamed:@"image.jpg"];
    photoTaken.height=self.canvas.height;
    photoTaken.origin=CGPointMake(0, 0);
    [self.canvas addImage:photoTaken];
    [self addGesture:PAN name:@"pan" action:@"movePhoto:"];

}
-(void)movePhoto:(UIPanGestureRecognizer *)recognizer {
    CGPoint thePoint=[recognizer locationInView:self.view];
    //C4Log(@"current position:%f,%f",thePoint.x, thePoint.y);
    if (thePoint.x>photoTaken.origin.x &&thePoint.x<photoTaken.origin.x+photoTaken.width && thePoint.y>photoTaken.origin.y &&thePoint.y<photoTaken.origin.y+photoTaken.height) {
        C4Log(@"touched inside+moved");
        [photoTaken move:recognizer];
    }

}

我只在用户实际标记图像而不在外面的任何地方时使用平移手势(那是if-statement) 它本身完美无缺。 然后我还有一个滑块,我附加缩放相同的图像:

-(void)sliderSetup{
    [self createAddSliderObjects];
    zoomSlider.minimumValue=0.52f;
    zoomSlider.maximumValue=10.0f;
    //scalefactor=1;
    zoomSlider.value=1.0f;

}
-(void)createAddSliderObjects{
    sliderLabel=[C4Label labelWithText:@"1.0"];
    sliderLabel.textColor=navBarColor;
    zoomSlider=[C4Slider slider:CGRectMake(0, 0, self.canvas.width-20, 20)];

    //positioning
    sliderLabel.center=CGPointMake(self.canvas.width/2,self.canvas.height-NavBarHeight-50);
    zoomSlider.center=CGPointMake(sliderLabel.center.x,sliderLabel.center.y+10);

    //set up action
    [zoomSlider runMethod:@"sliderWasUpdated:"
                   target:self
                 forEvent:VALUECHANGED];
    [self.canvas addObjects:@[sliderLabel, zoomSlider]];
}
-(void)sliderWasUpdated:(C4Slider*)theSlider{
    //update the label to reflect current scale factor
    sliderLabel.text=[NSString stringWithFormat:@"%4.2f", theSlider.value];
    [sliderLabel sizeToFit];

    //scale the image
    C4Log(@"slider:%f",theSlider.value);
    photoTaken.height=self.canvas.height*theSlider.value;
    photoTaken.center=self.canvas.center;
}

它本身也可以正常工作。 但是试图同时使用两者并不起作用。在这种情况下,只能平移图像,但滑块没有任何反应。它似乎没有任何触发器...... 有人建议我还能尝试什么吗?

1 个答案:

答案 0 :(得分:1)

问题在于您正在向canvas添加平移手势,并且C4Slider的控制方法实际上是平移手势。因此,附加到画布的手势优先,使用手势移动照片,并且不会将该手势传递到C4Slider以便移动。

简单的解决方案是将平移手势附加到C4Image,以便滑块和图像不会侦听相同的事件。

我已经折叠了一些功能,以使示例更容易理解。

#import "C4WorkSpace.h"

@implementation C4WorkSpace{
    C4Image * photoTaken;
    C4Slider * zoomSlider;
}

-(void)setup{

    photoTaken=[C4Image imageNamed:@"C4Sky.png"];
    photoTaken.width=self.canvas.width;
    photoTaken.origin=CGPointMake(0, 0);
    [self.canvas addImage:photoTaken];
    [photoTaken addGesture:PAN name:@"pan" action:@"move:"];

    zoomSlider=[C4Slider slider:CGRectMake(0, 0, self.canvas.width-20, 20)];
    [self.canvas addUIElement: zoomSlider];
    [zoomSlider runMethod:@"sliderWasUpdated:" target:self forEvent:VALUECHANGED];

}

-(void)sliderWasUpdated:(C4Slider*)theSlider{    
    photoTaken.height=self.canvas.height*theSlider.value;
    photoTaken.center=self.canvas.center;
}

@end