2

I'm trying to pan and zoom into an image. As the pan-gesture isn't available in C4 yet, I thought I could go with a slider for the beginning. So I'm adding the pan-gesture to the photo like this:

-(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];
    }

}

I'm using the pan gesture only when the user actually tabs the image and not anywhere outside (that's the if-statement up there) It works perfectly fine by itself. Then I also have a slider that I attached to scaling the same image:

-(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;
}

It also works fine by itself. But trying to use both at the same time doesn't work. In that case only panning the image works, but nothing happens to the slider ever. it seems that it's not getting any triggers ever.... Does someone have a suggestion what else I could try?

suMi
  • 1,536
  • 1
  • 17
  • 30

1 Answers1

1

The problem is that you are adding a pan gesture to the canvas and that the control method of the C4Slider is actually a pan gesture. So, the gesture that is attached to the canvas takes precedent, uses the gesture to move the photo and doesn't pass that gesture on to the C4Slider in order for it to move.

The simple solution is to attach the pan gesture to the C4Image so that the slider and the image don't listen for the same events.

I've collapsed some of the functions to make the example a little easier to follow.

#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
Adam Tindale
  • 1,239
  • 10
  • 26
  • I don't know if I'm doing anything wrong but if I run the example only the pan works, but I cannot change the slider values.... – suMi Oct 18 '13 at 08:43
  • Strange. If I paste this code into a new project it works just fine. Are you using it with new project or inside of your larger project? – Adam Tindale Oct 18 '13 at 15:30
  • I tried both and it didn't work... does it make any difference in which iOS/XCode/... I am? – suMi Oct 18 '13 at 16:16
  • It might. I'm testing with XCode 4.6 using iOS6. I notice you are on XCode 5. I haven't tested that yet. Sorry. – Adam Tindale Oct 18 '13 at 16:28
  • yeah.I'm in XCode 5 and iOS 7... so I could try using a stepper and see if that works... – suMi Oct 18 '13 at 17:32
  • I would try moving the image to the middle of the canvas for its initial position. – Adam Tindale Oct 19 '13 at 00:56
  • I solved the problem now with a stepper instead of the slider. It kind of works for my purposes but if someone can figure out the reason why this doesn't work in iOS7 it would be nice if you could solve the issue... thanks Adam for your help! – suMi Oct 21 '13 at 09:00