知道当在Facebookloginbutton被按下时
当有人按facebookloginbutton时,我想显示一个HUD。 目前我很难知道button被按下的时间。
我试图抓住所有的视图,但这不适用于Facebookbutton上的任何共振…
怎样才能知道什么时候按下Facebookloginbutton,打电话给我的HUD?
另外,它将很高兴知道Facebookloginbutton触发哪个function。 找不到
代码抓住水龙头,在LoginViewController.m(更新,工作解决scheme)
// workaround to find out when user tapped the login button -(void)addTapFinder { UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped)]; tapped.numberOfTapsRequired = 1; tapped.delegate = self; tapped.cancelsTouchesInView = NO; FBLoginView *fbButton = [[FBLoginView alloc] init]; UIView * fbView = (UIView *)fbButton; fbView = [self.view viewWithTag:fbloginViewTAG]; [fbView addGestureRecognizer:tapped]; DLog(@"found View %@", fbView); } -(void)tapped { DLog(@"tapped"); }
debugging器日志
[872:60b] -[LoginViewController FacebookLogin] [Line 283] loginview subviews: <FBShadowLabel: 0x16d17840; baseClass = UILabel; frame = (33 -1; 168 46); text = 'Connect Facebook'; autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x18136240>> [872:60b] -[LoginViewController showFBLogin] [Line 231] show login View [872:60b] -[LoginViewController tapped] [Line 250] tapped // tapped somewhere in self.view, but not the login button // tapped the login button, no "tapped" entry in the logfile
在完整的代码下面:
LoginViewController.h
#import <UIKit/UIKit.h> #import <FacebookSDK/FacebookSDK.h> #import "WebApi.h" #import "SVProgressHUD.h" @interface LoginViewController : UIViewController <FBLoginViewDelegate, UIGestureRecognizerDelegate> -(IBAction)toSurroundView; @property (nonatomic, strong) WebApi *swebapi; @property (nonatomic) BOOL isFirstFBLoginDone; @property (weak, nonatomic) IBOutlet UIView *loginView; @end
LoginViewController.m
#import "LoginViewController.h" @interface LoginViewController () @end @implementation LoginViewController static int const fbloginViewTAG = 203; -(void)vinit { self.swebapi = [[WebApi alloc] init]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"didRegisterUser" object:nil ]; self.isFirstFBLoginDone = FALSE; } - (void)viewDidLoad { [self vinit]; [super viewDidLoad]; [self showFBLogin]; } -(void)handleNotification:(NSNotification *)message { if ([[message name] isEqualToString:@"didRegisterUser"]) { [self toSurroundView]; } } -(IBAction)toSurroundView { [SVProgressHUD dismiss]; // connecting to Facebook, this is called to late! UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; UIViewController *surroundViewController = [mainStoryboard instantiateInitialViewController]; surroundViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; [self presentViewController:surroundViewController animated:YES completion:nil]; } #pragma mark - Facebook Things -(void)showFBLogin { UIView *connectView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; [self.view addSubview:connectView]; [self FacebookLogin]; [self addTapFinder]; DLog(@"show login View"); } // workaround to find out when user tapped the login button -(void)addTapFinder { UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped)]; tapped.numberOfTapsRequired = 1; tapped.delegate = self; tapped.cancelsTouchesInView = NO; FBLoginView *fbButton = [[FBLoginView alloc] init]; UIView * fbView = (UIView *)fbButton; fbView = [self.view viewWithTag:fbloginViewTAG]; [fbView addGestureRecognizer:tapped]; DLog(@"found View %@", fbView); } -(void)tapped { DLog(@"tapped"); } -(void)FacebookLogin { // https://developers.facebook.com/docs/ios/login-ui-control/ // https://developers.facebook.com/docs/ios/session/ FBLoginView *loginView = [[FBLoginView alloc] init]; loginView.delegate = self; loginView.tag = fbloginViewTAG; loginView.readPermissions = @[@"email", @"basic_info"]; loginView.frame = CGRectOffset(loginView.frame, (self.view.center.x - (loginView.frame.size.width / 2)), ([[UIScreen mainScreen] bounds].size.height - 80)); // change fb login button text UILabel *fblabel = [loginView subviews][1]; fblabel.text = @"Connect Facebook"; DLog(@"loginview subviews: %@", [loginView subviews][1]); [self.view addSubview:loginView]; [loginView sizeToFit]; } - (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user { [SVProgressHUD showWithStatus:@"Connecting with Facebook"]; if (! self.isFirstFBLoginDone) { #warning simple workaround, but not solving the source problem of twiced call from loginViewFetchedUserInfo if ([[SingletonClass sharedInstance] hasCredentials] == NO) { [self.swebapi registerUser:user]; } else { [self toSurroundView]; } self.isFirstFBLoginDone = TRUE; } // isFirstFBLoginDone } @end
一些可能会导致您的代码中的问题的东西 –
-
设置
UIGestureRecognizer
的委托,以强制识别多个手势识别器,特别是同时识别这个新的手势识别器和Facebookloginbutton的手势识别器):gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer
-
将您的
UIGestureRecognizer
的cancelsTouchesInView
属性设置为NO。 -
你应该将
FBLoginView
声明为FBLoginView
,并将UIView
fbButton
为FBLoginView
,而不是将你的指针初始化为FBLoginView
。 -
(最重要的)你应该将
UITapGestureRecognizer
添加到视图中,当你将它添加到button。
更改:
[self.view addGestureRecognizer:tapped];
至:
[fbButton addGestureRecognizer:tapped];
- 如何在startWithGraphPath Facebook iOS中标记好友
- 检索Facebook朋友列表
- 使用Facebook-SDK发布的video不能在iPhone的Facebook应用程序中播放
- 新的Facebook iOS应用的自定义URLscheme
- iOS Facebookgraphics请求多个图片大小
- iOS上的Facebook SDK 4.6loginbutton不使用Facebook应用程序进行身份validation
- iOS – Facebook – 您必须升级这个应用程序到Graph API v2.x
- 将Facebook SDK更新至3.5后无法打开活动会话
- Facebook的iOS SDK – 如何build立Xcode 4.3的静态库?