0

My requirment is to if i click on button1 corresponding images has to come in next view by swipe gestures,so for that i am sending sender value to the child view controller

viewcontroller.m

-(IBAction)cricket:(id)sender

{

  //cricket is a button
   NSString *str=[cricket currentTitle];
  ChildViewController *child=[[ChildViewController alloc]init];
   [child handleSwipe:str];

}

and here is

childviewcontroller.m

int imageIndex=4;
 - (IBAction)handleSwipe:(UISwipeGestureRecognizer *)sender 
 {
    if([sender isEqual:@"cricket"]))
{

    NSArray *images=[[NSArray alloc]initWithObjects:[UIImage imageNamed:@"exterior1.png"],      [UIImage imageNamed:@"cricket.png"],[UIImage imageNamed:@"floor_plan1.png"],Nil];
    UISwipeGestureRecognizerDirection direction=[(UISwipeGestureRecognizer *)sender direction];

    switch (direction) {
        case UISwipeGestureRecognizerDirectionLeft:
            imageIndex++;
            break;
        case UISwipeGestureRecognizerDirectionRight:
            imageIndex--;
        default:
            break;
    }
    imageIndex=(imageIndex<0) ? ([images count]-1):
    imageIndex % [images count];
    [images objectAtIndex:imageIndex];

}

i was badly strucked up with this thing ,please help me

  • You need to set two different SwipeGestureRecognizer if you want to have different actions according to the swipe direction, as seen in : http://stackoverflow.com/a/5322437/900937 – Nerkatel Aug 29 '13 at 10:02

2 Answers2

0

here direction is not a getter method only setter method that's why application getting crashed.

and here (UISwipeGestureRecognizer *)sender it gives image view object once check your code .

Edit:-

we can set direction to the UISwipeGestureRecognizer by using direction property. if you tried like this _swiper.direction == UISwipeGestureRecognizerDirectionLeft every time it'l be false because here you are trying to get the direction but it won't work.

For recognizing swipe direction you can add different gestures for different directions.The direction property only defines the allowed directions that are recognized as swipes, not the actual direction of a particular swipe.

UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFromLeft)];
[leftRecognizer setDirection: UISwipeGestureRecognizerDirectionLeft];
[[self view] addGestureRecognizer:leftRecognizer];
[leftRecognizer release];

UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFromRight)];
[rightRecognizer setDirection: UISwipeGestureRecognizerDirectionRight];
[[self view] addGestureRecognizer:rightRecognizer];
[rightRecognizer release];
EDIT:

-(void)handleSwipeFromRight{
NSLog(@"swipe from right");
}
and

-(void)handleSwipeFromLeft{
NSLog(@"swipe from Left");
}
Balu
  • 8,470
  • 2
  • 24
  • 41
  • what you need exactly tell me? – Balu Aug 29 '13 at 10:02
  • actually i have taken 4 buttons in my view controller ,if i click the 1st button corresponding gestures has to come in next view, i am not able to getting how can i send the currentbutton title throgh(id)sender to the nextview – user2727854 Aug 29 '13 at 10:51
0

I do not really understand what you are trying to do, but there are many things wrong with your code, let me start here:

NSString *str= [arr objectAtIndex:0];

You are grabbing the first object from the array and placing it in a NSString but it's really an UIImage.

Then in you childviewcontroller.m you are checking the sender is equal to an other UIImage but the equals here will check pointer and since [UIImage imageNamed:] will often return a cached image this might work. But it will might not.

The you cast the sender to UISwipeGestureRecognizer:

UISwipeGestureRecognizerDirection direction=[(UISwipeGestureRecognizer *)sender direction];

But your if you are checking if the sender is UIImage and then you cast it to a UISwipeGestureRecognizer. which it is not. There for your code crashes.

Then you do this:

img.image=[UIImage imageNamed:[images objectAtIndex:imageIndex]];

The object returned by [images objectAtIndex:imageIndex] is already a UIImage and [UIImage imageNamed:[images objectAtIndex:imageIndex]] will always return nil in this case. You can just use:

img.image= [images objectAtIndex:imageIndex];

I would suggest you rewrite you code to use string only and not UIImage objects since these take up more memory. Then a the last step load the image with imageNamed: method.

rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • actually i have taken 4 buttons in my view controller ,if i click the 1st button corresponding gestures has to come in next view this is my requirment & for buttons i have taken backgroundimage ,so that i am comparing with images – user2727854 Aug 29 '13 at 10:15
  • I've just pointed out many mistakes in your code, please try and fix them. We are not here to code your requirements, we are here to help you when you get stuck. – rckoenes Aug 29 '13 at 10:16
  • Also you are misunderstanding gestures, they are movements the users finder makes on a view, not a animation move that can be applied to a view. – rckoenes Aug 29 '13 at 11:10