关于UIContro类几个方法:
// add target/action for particular event. you can call this multiple times and you can specify multiple target/actions for a particular event.
// passing in nil as the target goes up the responder chain. The action may optionally include the sender and the event in that order
// the action cannot be NULL. Note that the target is not retained.
- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
// remove the target/action for a set of events. pass in NULL for the action to remove all actions for that target
- (void)removeTarget:(nullable id)target action:(nullable SEL)action forControlEvents:(UIControlEvents)controlEvents;
// get info about target & actions. this makes it possible to enumerate all target/actions by checking for each event kind
- (NSSet *)allTargets; // set may include NSNull to indicate at least one nil target
- (UIControlEvents)allControlEvents; // list of all events that have at least one action
- (nullable NSArray<NSString *> *)actionsForTarget:(nullable id)target forControlEvent:(UIControlEvents)controlEvent; // single event. returns NSArray of NSString selector names. returns nil if none
// send the action. the first method is called for the event and is a point at which you can observe or override behavior. it is called repeately by the second.
- (void)sendAction:(SEL)action to:(nullable id)target forEvent:(nullable UIEvent *)event;
- (void)sendActionsForControlEvents:(UIControlEvents)controlEvents; // send all actions associated with events
最后一个方法
- (void)sendActionsForControlEvents:(UIControlEvents)controlEvents;
调用这个方法的控件的action响应指定的controlEvents事件,也就是让facebook的原生分享按钮的动作响应自定义的分享按钮的事件。
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKShareKit/FBSDKShareKit.h>
@interface NIPShareView ()
@property (nonatomic, strong) FBSDKShareButton *fbShareBtn;
@end
@implementation NIPShareView
- (instancetype)initWithFrame:(CGRect)frame
{
frame = [UIScreen mainScreen].bounds;
if (self = [super initWithFrame:frame]) {
[self setupUI];
[self setFBShareEvent];
}
return self;
}
- (void)setupUI
{
NIPQuickLoginButton * fbShareBtn = [self createShareBtn:[UIImage imageNamed:@"profile_login_facebook"] title:@"facebook" tag:NIPShareTypeFb];
[self.topScollView addSubview:fbShareBtn];
[self.topBtns addObject:fbShareBtn];
}
- (NIPQuickLoginButton *)createShareBtn:(UIImage *)image title:(NSString *)title tag:(NIPShareType)type
{
NIPQuickLoginButton * shareBtn = [NIPQuickLoginButton buttonWithType:UIButtonTypeCustom];
[shareBtn setImage:image forState:UIControlStateNormal];
[shareBtn setImage:image forState:UIControlStateHighlighted];
[shareBtn setTitle:title forState:UIControlStateNormal];
shareBtn.titleLabel.font = [UIFont systemFontOfSize:12];
[shareBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
shareBtn.tag = type;
[shareBtn addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
return shareBtn;
}
- (void)setFBShareEvent
{
FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
SDKShareButton *shareButton = [[FBSDKShareButton alloc] init];
shareButton.shareContent = content;
self.fbShareBtn = shareButton;
}
- (void)shareClick:(UIButton *)button
{
if (button.tag == NIPShareTypeFb) {
[self.fbShareBtn sendActionsForControlEvents:UIControlEventTouchUpInside];
[self dismiss];
}}
@end
效果:(耶塔嘛丑的一笔)
fbshare.gif
who care.jpeg