UISwipeGestureRecognizer Not Performing Method - ios

I am having a difficult time getting a Swipe Gesture Recognizer to work on my app. Here is the Hierarchy of it all.
App's root view is a UINavigationController which has class ViewController as visible for the first view seen. I have a UIButton which will fire a movie that loops until I tap it twice which will have the navigation controller push a new ViewController that I have made called PUPPETS1 onto the screen. This VC has its own xib. The xib has a UIImageView. What I want to have happen is to start playing a movie once I swipe up on the screen, but that never happens, and the console never shows my NSLog from the 2nd VC's method.
- (void)loopVideo {
NSURL *videoURL = [[NSBundle mainBundle] URLForResource:#"warpspeed" withExtension:#"mov"];
UIView *patternView = [[UIView alloc] initWithFrame:self.view.bounds];
patternView.backgroundColor = [UIColor blackColor];
[self.moviePlayer2.backgroundView addSubview:patternView];
self.moviePlayer2 = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[self.moviePlayer2 setControlStyle:MPMovieControlStyleDefault];
self.moviePlayer2.controlStyle = MPMovieControlStyleNone;
self.moviePlayer2.scalingMode = MPMovieScalingModeAspectFill;
self.moviePlayer2.movieSourceType = MPMovieSourceTypeFile;
[self.moviePlayer2 setAllowsAirPlay:YES];
self.moviePlayer2.view.frame = self.view.frame;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(puppetOne)];
tapGesture.numberOfTapsRequired = 2;
tapGesture.numberOfTouchesRequired = 1;
UIView *aView = [[UIView alloc] initWithFrame:self.moviePlayer2.backgroundView.bounds];
[aView addGestureRecognizer:tapGesture];
[self.view.window addSubview:aView];
[self.view addSubview:self.moviePlayer2.view];
self.moviePlayer2.repeatMode = MPMovieRepeatModeOne;
[self.moviePlayer2 play];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
In the 2nd VC, the PUPPETS1 one:
- (void)viewDidLoad {
[super viewWillAppear:YES];
UISwipeGestureRecognizer * swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(playPuppets)];
swipeRec.direction = UISwipeGestureRecognizerDirectionUp;
UIView *aView2 = [[UIView alloc] initWithFrame:self.view.bounds];
[aView2 addGestureRecognizer:swipeRec];
[self.view addSubview:aView2];
// Do any additional setup after loading the view from its nib.
}
-(void)playPuppets {
NSLog(#"PLAYING");
NSURL *videoURL = [[NSBundle mainBundle] URLForResource:#"SundayPuppets" withExtension:#"m4v"];
//filePath may be from the Bundle or from the Saved file Directory, it is just the path for the video
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
//[playerViewController.player play];//Used to Play On start
[self presentViewController:playerViewController animated:YES completion:nil];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}

The frame of the view to which you're attaching the gr depends on it's parent view's bounds, and those aren't initialized yet in viewDidLoad.
Move the setup to after layout is complete (and tweak it to run just once, on the first layout change), i.e.
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
UIView *swipeView = [self.view viewWithTag:999];
// only do this if we haven't done it already
if (!swipeView) {
// now that self.view.bounds is initialized...
swipeView = [[UIView alloc] initWithFrame:self.view.bounds];
swipeView.tag = 999;
// the rest of your OP setup code is fine, and goes here
UISwipeGestureRecognizer * swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(playPuppets)];
swipeRec.direction = UISwipeGestureRecognizerDirectionUp;
[swipeView addGestureRecognizer:swipeRec];
[self.view addSubview:swipeView];
NSLog(#"%#", swipeView);
}
}
EDIT The code above attached the gesture recognizing view correctly, providing a fix in the second view controller. It turns out that the other problem was the previous view controller was not properly removing the (deprecated) MPMoviePlayer, resulting in touches not functioning on the pushed vc. The entire reworked ViewController.m can be found in the chat linked below, but the fix for the touches issue was here...
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.moviePlayer stop];
[self.moviePlayer.view removeFromSuperview];
self.moviePlayer = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

Related

How to make MPMoviePlayer show Controls after a tap?

This is my code and it does not work on tap. This is how I see my code: The control style of the player is initially hidden. The player view has a tap gesture recogniser in it. When it taps, I want to show the Controls appear :
- (void)viewDidLoad {
_player = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[_player.view setFrame:CGRectMake(0, 0, 540, 250)];
[self.view addSubview:_player.view];
_player.view.center = self.view.center;
//Control style is None
_player.controlStyle = 0;
//Gesture
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[_player.view addGestureRecognizer:tap];
tap.numberOfTapsRequired = 1;
_player.scalingMode = 2;
[_player prepareToPlay];
[_player shouldAutoplay];
}
- (void)handleTap:(UITapGestureRecognizer *)gesture {
// ControlStyle changed to ShowEmbedded
_player.controlStyle = 1;
}
I have added the tap recogniser in the view of the player area, it should show but sadly it does not. I have added the super viewDidLoad in my actual code. That is not the problem.
Make a button. When the button is tapped, the video should be paused. Then change the control style within. The user then has to click the to see the controls.
- (IBAction)showControls:(id)sender {
[_player pause];
_player.controlStyle = MPMovieControlStyleEmbedded;
}

Add image to subclassed UIImageView

I am trying to add a tap gesture from a subclassed UIImageView and then control the tap from the View Controller. I am not getting any compiling errors but "addSubview" is not displaying any image. How can make the UIImageView to be displayed?
If I try to control the tap and pan gestures from the subclassed UIImageVIew I have no problems but I would like to control these functions from the View Controller
Relevant code looks like this.
UIImageView subclass    
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
//self.userInteractionEnabled = YES;
previewController = [[PreviewController alloc]init];
[previewController self];
[self addSubview:character];
// Tap Initialization code
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:(PreviewController *)self.previewController action:#selector(addCharacter:)];
[self addGestureRecognizer:tap];
self.userInteractionEnabled = YES;
}
return self;
}
View Controller
- (void)addCharacter:(UITapGestureRecognizer *)t
{
NSLog(#"add character");
imageNSArray = [NSMutableArray array];
uiImg = [UIImage imageNamed:#"homer.png"];
CGPoint loc = [t locationInView:self.view];
character = [[UIImageView alloc] initWithImage:uiImg];
character.center = loc;
[imageNSArray addObject:character];
//Locate the imageNSArray on frameImageView area only.
[self.view addSubview:character];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panCharacter:)];
[self.character addGestureRecognizer:pan];
character.userInteractionEnabled = YES;
}
put a view behind your subclassed imageView. Apply the gestureRecognition to the new View.
You can control your tap gesture from new view. Let me know if i am not clear, or if more info needed.
I think what you want is.. .you want to show a image when user taps on screen.
Do following steps:
1. drag & drop a tap gesture recognizer on default view of your view controller.
2. connect (ctrl +) gestureRecognizer with ViewController.m
Take a look at this code. (Just modified your code)
- (IBAction)onTap:(UITapGestureRecognizer *)sender {
NSLog(#"add character");
UIImage * uiImg = [UIImage imageNamed:#"sel.png"];
CGPoint loc = [sender locationInView:self.view];
UIImageView * character = [[UIImageView alloc] initWithImage:uiImg];
character.center = loc;
[self.view addSubview:character];
}
Let me know if this is not what you want…
Edit
better go for this code… No need to do above steps.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(onTap:)];
[self.view addGestureRecognizer:tap];
self.view.userInteractionEnabled = YES;
}
- (void)onTap:(UITapGestureRecognizer *)sender
{
NSLog(#"add character");
UIImage * uiImg = [UIImage imageNamed:#"sel.png"];
CGPoint loc = [sender locationInView:self.view];
UIImageView * character = [[UIImageView alloc] initWithImage:uiImg];
character.center = loc;
[self.view addSubview:character];
}

How to disable UITapGestureRecognizer when sellect photo from library?

I'm using UITapGestureRecognizer
This is my code:
Home.m:
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapAnim:)];
[self.view addGestureRecognizer:tapGesture];
UIButton *buttontest = [[UIButton alloc] init];
buttontest.backgroundColor = [UIColor whiteColor];
buttontest.frame = CGRectMake(0, 80, 40, 40);
[buttontest addTarget:self action:#selector(test:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttontest];
[self.view bringSubviewToFront:buttontest];
}
- (void)test: (UIButton*)aButton {
// TakePhoto *mvc = [[TakePhoto alloc]initWithNibName:#"TakePhoto" bundle:Nil];
// [self.navigationController pushViewController:mvc animated:YES];
//
// [self.view removeFromSuperview];
if (self.companyController) {
self.companyController = nil;
}
self.companyController = [[TakePhoto alloc] initWithNibName:#"TakePhoto" bundle:nil];
UIView *viewSuper = [[IQAppDelegate shareInstance] currentVisibleController].view;
UIViewController *profile = self.companyController;
profile.view.frame = viewSuper.frame;
[viewSuper addSubview:profile.view];
profile.view.frame = CGRectMake(viewSuper.frame.origin.x, viewSuper.frame.size.height, profile.view.frame.size.width, profile.view.frame.size.height);
[UIView beginAnimations:nil context: nil];
[UIView setAnimationDuration:0.35];
profile.view.frame = CGRectMake(viewSuper.frame.origin.x, viewSuper.frame.origin.x, profile.view.frame.size.width, profile.view.frame.size.height);
[UIView commitAnimations];
}
}
- (void) tapAnim: (UITapGestureRecognizer*)gestureRecognizer {
// Show something
}
TakePhoto.m
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
picker.allowsEditing = NO;
[(UIViewController *)self.delegate presentModalViewController:picker animated:YES];
I add a view:Takephoto front of Home(i'm not use "push"), like this:
--->Home
--->Take photo (like a popUp show): it has 2 buttons "Choose photo from library" and "Close"
When i use function "Choose photo from gallery", i cant choose a photo and UITapGestureRecognizer is always show.
How to disable UITapGestureRecognizer when choose photo from gallery?
P/S:sorry about my english.
Gesture recognizers have the enabled property. Set this to NO to disable the gesture recognizer. To make this easier you should keep a reference to the tap gesture recognizer using an instance variable.
you can set tapGesture.enabled=NO before you choose photos.
I think you need to implement this below delegate method :-
- (BOOL)gestureRecognizer:
(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch
When you look on the documentation it will return YES (by default) to allow the gesture recognizer to examine the touch object, NO to prevent the gesture recognizer from seeing this touch object. For more details follow this https://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIGestureRecognizerDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIGestureRecognizerDelegate/gestureRecognizer:shouldReceiveTouch:

MpMovieplayerController tap gesture recognizer doesn't trigger when in fullscreen

I´m trying to use UITapGestureRecognizer in order to handle the taps on my fullscreen video. If I omit [self.player setFullscreen:YES animated:NO]; it works, but then my video won't scale to fit the screen.
From my .m:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *videoPath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"mov"];
player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:videoPath]];
player.shouldAutoplay = NO;
player.view.frame = self.view.bounds;
player.scalingMode = MPMovieScalingModeAspectFit;
player.controlStyle = MPMovieControlStyleNone;
player.fullscreen = YES;
self.player = player;
[self.player prepareToPlay];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
UIView *aView = [[UIView alloc] initWithFrame:player.view.bounds];
[aView addGestureRecognizer:tapGesture];
[self.player.view addSubview:aView];
}
- (IBAction)playMovie:(id)sender {
//add the MPMoviePlayerViewController to this view (as subview)
//Play movie
[self.view addSubview:self.player.view];
[self.player setFullscreen:YES animated:NO]; //commenting out this will make it work
[self.player play];
}
- (void)handleTap:(UITapGestureRecognizer *)recognizer {
NSLog(#"tap tap");
}
From my .h:
#property (retain, nonatomic) MPMoviePlayerController *player;
- (void)handleTap:(UITapGestureRecognizer *)recognizer;
You can try this:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(willEnterFullScreen:)
name:MPMoviePlayerWillEnterFullscreenNotification
object:nil];
- (void)willEnterFullScreen:(NSNotification*)notification
{
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
UIView *aView = [[UIView alloc] initWithFrame:self.player.backgroundView.bounds];
[aView addGestureRecognizer:tapGesture];
[self.view.window addSubview:aView];
}
and then remove your subview when MPMoviePlayerWillExitFullscreenNotification is posted
In my comment, I drafted how to get that covered when using proper fullscreen ([self.player setFullscreen:YES animated:NO];).
I would suggest that instead you simply resize the player view to cover the entire screen by setting its frame accordingly.
You initialising code would have to get rid of that player.fullscreen = YES;, but that I guess is obvious by now.

Can't successfully removeGestureRecognizer iOS

I have a button in my iPad app, when its pressed it addGestureRecognizer's. When pressed again, its supposed to remove them.
My code is in an if block, and I know the right block of code is runned because NSLog print the right log message.
I'm adding the recognizers like this:
[self.view addGestureRecognizer:panRecognizer];
[self.view addGestureRecognizer:pinchRecognizer];
[self.view addGestureRecognizer:rotationRecognizer];
[self.view addGestureRecognizer:tapRecognizer];
And trying to remove them like this:
[self.view removeGestureRecognizer:panRecognizer];
[self.view removeGestureRecognizer:pinchRecognizer];
[self.view removeGestureRecognizer:rotationRecognizer];
[self.view removeGestureRecognizer:tapRecognizer];
This is my whole function:
http://www.pastelib.com/show/ZlICyb9Q
It does not work and I cant figure out why, do you guys have any suggestions?
Thanks in advance for all the help and suggestions :)
Your GestureReconizers are declared locally to the -showMenu: method.
So each time your button is pressed, you create new GestureReconizers, so you didn't try to remove previous reconizers, but you try to remove newly created ones.
Change the scope of your reconizers objects, and your problem will be fixed.
Cheers.
Edit with code sample :
-(IBAction) showMenu:(id) sender {
if([self.view.subviews containsObject:menuView]) {
NSLog(#"remove gestures!");
// Dismiss menu
[menuView removeFromSuperview];
// Remove gestures
[self.view removeGestureRecognizer:panRecognizer];
[self.view removeGestureRecognizer:pinchRecognizer];
[self.view removeGestureRecognizer:rotationRecognizer];
[self.view removeGestureRecognizer:tapRecognizer];
}else{
// Create menu
NSLog(#"add gestures!");
panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panDetected:)];
pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinchDetected:)];
rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotationDetected:)];
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected:)];
tapRecognizer.numberOfTapsRequired = 2;
// Make gestures work simultaneously
panRecognizer.delegate = self;
pinchRecognizer.delegate = self;
rotationRecognizer.delegate = self;
// Set width and height if empty
self.menuController = [[menuController alloc] initWithNibName:#"menuController" bundle:nil];
if(screenWidth == 0) {
UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if(currentOrientation == 1 || currentOrientation == 2) {
// portrait
screenHeight = self.menuController.view.frame.size.height;
screenWidth = self.menuController.view.frame.size.width;
}else{
// Landscape
screenHeight = self.menuController.view.frame.size.width;
screenWidth = self.menuController.view.frame.size.height+20; //height+20px for the status bar
}
}
int menuHeight = 80;
self.menuController.view.frame = CGRectMake(0,screenHeight-(menuHeight-20), screenWidth, menuHeight);
self.menuView = self.menuController.view;
[self.menuController setDelegate:self];
[self addChildViewController:self.menuController];
[self.menuController didMoveToParentViewController:self];
[self.view addSubview:menuView];
// Add image gestures
[self.view addGestureRecognizer:panRecognizer];
[self.view addGestureRecognizer:pinchRecognizer];
[self.view addGestureRecognizer:rotationRecognizer];
[self.view addGestureRecognizer:tapRecognizer];
[self.view bringSubviewToFront:menuButton];
}
}
I did this in my project and it worked:
while (cellMainView.descripcionArticulo.gestureRecognizers.count) {
[cellMainView.descripcionArticulo removeGestureRecognizer:[cellMainView.descripcionArticulo.gestureRecognizers objectAtIndex:0]];
}

Resources