1

I have added a uiswitch and want to move it to another position via gesture. but uipangesture is not working on uiswitch.. below is my code

    UISwitch *swich = [[UISwitch alloc]initWithFrame:CGRectMake(20, 20, 40, 50)];

    swich.userInteractionEnabled = YES;

    UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] 
                                       initWithTarget:self 
                                       action:@selector(switchdraged:)];
    [swich addGestureRecognizer:gesture];

    [self.view addSubview:swich];

function switchdraged is not being called..

below is switchdraged function

- (void)switchdraged:(UIPanGestureRecognizer *) gesture1
{
        UISwitch *swich = (UISwitch *)gesture1.view;
        CGPoint translation = [gesture1 translationInView:swich];
        swich.center = CGPointMake(swich.center.x + translation.x, 
                                swich.center.y + translation.y);
        // reset translation
        [gesture1 setTranslation:CGPointZero inView:swich];
}
KsK
  • 675
  • 1
  • 10
  • 22

1 Answers1

1

I think UIPanGestureRecognizer will not work with UISwitch.

The class reference says

UIPanGestureRecognizer is a concrete subclass of UIGestureRecognizer that looks for panning (dragging) gestures. The user must be pressing one or more fingers on a view while they pan it.

SO UISwitch is probably overridden for changing the state

I have created a sample application

The same code working well with UILabel instead of UISwitch. I have discussed with Abizern. He told me that It's probably overridden for changing the state.

Please describe well what you need to do with UISwitch.

This way may help you

1.Create a switch inside a view and add as a subview

- (void)viewDidLoad
{  

    UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];  
    containerView.backgroundColor = [UIColor blackColor];
    UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] 
                                       initWithTarget:self 
                                       action:@selector(switchdraged:)];
    [containerView addGestureRecognizer:gesture];


    UISwitch *swich = [[UISwitch alloc]initWithFrame:CGRectMake(20, 20, 40, 50)];    
    swich.userInteractionEnabled = YES;



    [containerView addSubview:swich];    

    [self.view addSubview:containerView];
    [self.view bringSubviewToFront:containerView];


    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)switchdraged:(UIPanGestureRecognizer *) gesture1
{
    NSLog(@"triggered");

    UIView *swich = (UIView *)gesture1.view;
    CGPoint translation = [gesture1 translationInView:swich];
    swich.center = CGPointMake(swich.center.x + translation.x, 
                               swich.center.y + translation.y);
    // reset translation
    [gesture1 setTranslation:CGPointZero inView:swich];
}

or

This link may help you

Draggable Buttons and Labels

Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
  • if that is the case than putting the switch in a UIView and than adding the UIPanGesture to it should work.. – Ankit Srivastava Aug 15 '12 at 11:50
  • @AnkitSrivastava: i have added that too.. :) – Shamsudheen TK Aug 15 '12 at 12:09
  • hi ramshad could you please solve my this problem? i will be highly obliged http://stackoverflow.com/questions/11975022/uilongpressgesturerecognizer-issue-with-uibutton-and-uilabel – KsK Aug 15 '12 at 18:35
  • yeah.. i am going to post answer.. before that, if my solution works, u can accept my answer through ticking the tick symbol(which displays top left of my answer or below the up-vote) – Shamsudheen TK Aug 16 '12 at 04:25