I have a button which I would like to show and hide as the iOS MPMoviePlayer controls do when the video is tapped. Is there an better way to do this other than making my own gesture recognizer and timer for hiding/showing the button when the apple ones do?
That would be your best option. Here's roughly what it could look like:
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(videoTapped:)];
[videoView addGestureRecognizer:recognizer];
...
- (void)videoTapped:(UITapGestureRecognizer *)recognizer {
[UIView animateWithDuration:0.25f animations:^{
buttonView.alpha = (CGFloat)!buttonView.alpha;
}];
}
Let me know if you have any other questions.
Related
I'm working on a GLKViewController based game which interprets taps and swipes as game controls. I want to support two-player mode by letting the first player tap or swipe on the left side of the screen, and letting the second player tap or swipe on the right side of the screen. In a perfect world, I'd like the gesture recognizers to work even if the swipes are sloppy and go past the centerline of the screen (with the starting point of the swipe being used to determine which player gets the input).
What would be the best way to implement this? Can I lay down a gesture recognizer on the left half of the screen, and another one on the right side of the screen? Will two separate recognizers work properly together even if both sides are being tapped/swiped rapidly at the same time? Or should I create a full-screen recognizer and parse the swipes and taps entirely on my own? I don't have experience with gesture recognizers so I don't know what the preferred approach is or how well they work when you have more than one being swiped on simultaneously.
I ended up making two UIViews overlaid on top of my GLKView, one on the left side of the screen and one on the right. Each view has a UIPanGestureRecognizer and a UILongPressGestureRecognizer (the long-press recognizer is basically a more flexible tap—I needed to use it to reject some gestures from being interpreted both as a pan and a tap at the same time). This worked magnificantly.
- (void)viewDidLoad
{
[super viewDidLoad];
// Add tap and pan gesture recognizers to handle game input.
{
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handleLeftSidePan:)];
panRecognizer.delegate = self;
[self.leftSideView addGestureRecognizer:panRecognizer];
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleLeftSideLongPress:)];
longPressRecognizer.delegate = self;
longPressRecognizer.minimumPressDuration = 0.0;
[self.leftSideView addGestureRecognizer:longPressRecognizer];
}
{
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handleRightSidePan:)];
panRecognizer.delegate = self;
[self.rightSideView addGestureRecognizer:panRecognizer];
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleRightSideLongPress:)];
longPressRecognizer.delegate = self;
longPressRecognizer.minimumPressDuration = 0.0;
[self.rightSideView addGestureRecognizer:longPressRecognizer];
}
}
- (void)handleLeftSidePan:(UIPanGestureRecognizer *)panRecognizer
{
[self handleGameScreenPan:panRecognizer withVirtualController:&g_iPadVirtualController[0]];
}
- (void)handleRightSidePan:(UIPanGestureRecognizer *)panRecognizer
{
[self handleGameScreenPan:panRecognizer withVirtualController:&g_iPadVirtualController[1]];
}
- (void)handleLeftSideLongPress:(UILongPressGestureRecognizer *)longPressRecognizer
{
[self handleGameScreenLongPress:longPressRecognizer withVirtualController:&g_iPadVirtualController[0]];
}
- (void)handleRightSideLongPress:(UILongPressGestureRecognizer *)longPressRecognizer
{
[self handleGameScreenLongPress:longPressRecognizer withVirtualController:&g_iPadVirtualController[1]];
}
I have already tried by this code but gesture not detect.
UISwipeGestureRecognizer *mSwipeUpRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(up_downBtn_waytosalon)];
[mSwipeUpRecognizer setDirection:UISwipeGestureRecognizerDirectionUp];
[self.duration_distanceView addGestureRecognizer:mSwipeUpRecognizer];
First Off, you need to always pass the gesture recognizer to your selector, otherwise you can't determine the state of your gesture.
Other than that first check if that view has userInteractionEnabled to YES, I'm guessing that view doesn't get any user interaction.
YOUR duration_distanceView view is imageView then add below line your code.
self.duration_distanceView.userInteractionEnabled = YES;
Try this code :
UISwipeGestureRecognizer *mSwipeUpRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(up_downBtn_waytosalon)];
self.duration_distanceView.userInteractionEnabled = YES;
[mSwipeUpRecognizer setDirection:UISwipeGestureRecognizerDirectionUp];
[self.duration_distanceView addGestureRecognizer:mSwipeUpRecognizer];
Initialize swipe gesture like this:
UISwipeGestureRecognizer *mSwipeUpRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(up_downBtn_waytosalon:)];
mSwipeUpRecognizer.direction = (UISwipeGestureRecognizerDirectionUp);
[self.duration_distanceView addGestureRecognizer:mSwipeUpRecognizer];
Implement the gesture action like this:
- (void) up_downBtn_waytosalon:(UISwipeGestureRecognizer *)sender {
if ( sender.direction | UISwipeGestureRecognizerDirectionUp )
NSLog(#"Its swipe up");
}
Hope this code helps.
using: objective-C
I have a tableView with rows. I want that user can move cell a little aside and additional action shown to him.
What was done:
Currently create separately table in xib file and cell in xib file.
Cell is very simple
I want to move viewWIthLabel.
In cell class file for using animation I use next code
- (void)awakeFromNib
{
UISwipeGestureRecognizer * leftGestrudeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeLeft:)];
leftGestrudeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
UISwipeGestureRecognizer * rightGestrudeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRight:)];
rightGestrudeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self addGestureRecognizer:rightGestrudeRecognizer];
[self addGestureRecognizer:leftGestrudeRecognizer];
}
and actions:
- (IBAction)swipeLeft:(id)sender{
NSLog(#"swipeL");
[UIView animateWithDuration:0.5 animations:^{
self.viewWIthLabel.frame = CGRectMake(-100, 0, self.viewWIthLabel.frame.size.width, self.viewWIthLabel.frame.size.height);
} ];
}
- (IBAction)swipeRight:(id)sender{
NSLog(#"swipeR");
[UIView animateWithDuration:.5 animations:^{
self.viewWIthLabel.frame = CGRectMake(0, 0, self.viewWIthLabel.frame.size.width, self.viewWIthLabel.frame.size.height);
}];
}
So idea to swipe the cell and move it with animation for some distance to show a hidden button.
Result of this code - almoust like I want:
But if you start to scroll the tableView you can get duplicates of "swiped cells" in random (depending on scrolling speed) positions:
Any idea why it happened?
You might have to clear/reset the swiped state of the cell in – tableView:cellForRowAtIndexPath:. Since the cells get re-used (I am assuming you are doing that as would be required for good performance of the tableview). Probably something like this -
self.viewWIthLabel.frame = CGRectMake(0, 0, self.viewWIthLabel.frame.size.width, self.viewWIthLabel.frame.size.height);
kindly tell me which gesture they are using in my given screen shoot. If there is any example code or similar to this example then please tell me. I search over google with more than 15 different queries but did find any example like this .
As you can see they are not using a full page swipe but half.
Please check these two screen shoots
Thanks
You can use simple view animation for the same.
-(IBAction)tabPressed:(id)sender
{
if([sender isSelected])
{
sender.selected=NO;
[UIView beginAnimations:#"animation" context:NULL];
[UIView setAnimationDuration:0.5];
//set here frame to go down
[UIView commitAnimations];
}
else
{
sender.selected=YES;
[UIView beginAnimations:#"animation" context:NULL];
[UIView setAnimationDuration:0.5];
//set here frame to go up
[UIView commitAnimations];
}
}
Seems like there's a low-level work with gestures based on touchesBegan: and touchesMoved: methods.
Just Create A UITapGesture like this
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapFrom:)];
And Do Something here For Hide According You ...
- (void) handleTapFrom: (UITapGestureRecognizer *)recognizer
{
//Code to handle the gesture
}
Could be everything:
a simple button that trigger a slide up animation
if you can drag it and it stays attached to the finger is a pan Gesture recognizer
if the movement is a swipe, it is a swipe gesture recognizer
I have a large button that swiping up and down on it fires some methods using simple gestures.
A single tap on it fires up TouchUpInside event with largestButtonSingleTap.
My problem is if I tap the largeButton and keep it pressed, then after a second swiping up the swiping gesture won't work. Trying to figure out why.
I have a few other events on that button such as DragInside , TouchDown , DragExit , TouchCancel. I cancelled all of them and stayed with an empty method (commented out everything inside largestButtonSingleTap) but still, if the button is pressed > small pause > swipeUp > won't fire.
Thanks
UISwipeGestureRecognizer *upRecognizer;
upRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeUp:)];
[upRecognizer setDirection: UISwipeGestureRecognizerDirectionUp];
[_largestButton addGestureRecognizer:upRecognizer];
- (IBAction)largestButtonSingleTap:(UIButton *)sender
{ //some more code here
[UIView animateWithDuration:0.4 animations:^() {
...
}completion:^(BOOL finished){}];
...
}
-
EDIT:
After reading the answers suggested here I've started a new project thinking that there's something wrong with my code but it still doesn't work. Here is the new project http://pastebin.com/yz7NaqUD
UISwipeGestureRecognizer is Discrete Gestures, so you can not do that.
https://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizer_basics/GestureRecognizer_basics.html
Touchdown can go with other Continuous Gesture like Pan, pinch, rotation.
Try another way,subclass UIButton and implement touchesMove.
Refer:iOS Custom gesture recognizer measure length of longpress
the animation block may be blocking user interaction. Try using the allowUserInteraction animation option in your largestButtonSingleTap: method.
[UIView animateWithDuration:0.4 delay:0.0f options:UIViewAnimationOptionAllowUserInteraction animations:^{
//
} completion:^(BOOL finished) {
//
}];
Try setting the delegate to your gesture recognizer and implementing:
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
return YES;
}