分享到微信、使用者再切回 App 時,系統預設會跳一個「發送成功」的 toast。這個提示往往和自家 App 的視覺風格格格不入,而且會遮掉我們想呈現的內容。以下示範兩件事:把預設提示關掉,以及換上自訂的回饋畫面。
只需要在分享時加上一行設定:
[UMSocialConfig setFinishToastIsHidden:YES position:UMSocialiToastPositionCenter];
思路是監聽友盟的分享完成回調,判斷 responseCode 是否為 200,成功的話就在 window 上疊一層自製的遮罩與提示卡;失敗則走錯誤提示。
// 點擊返回 App 時會呼叫這個方法
- (void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response {
// 回傳 200 代表分享成功
if (response.responseCode == 200) {
// 自 add 一層遮罩,並掛上點擊手勢以便隨時關閉
self.mask2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Height)];
self.mask2.backgroundColor = [[UIColor colorWithHexColorString:@"000000"] colorWithAlphaComponent:0.5];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[self.mask2 addGestureRecognizer:tap];
[self.view.window addSubview:self.mask2];
// 遮罩上放提示卡
self.showView = [[UIView alloc] init];
self.showView.frame = CGRectMake(32, kScreen_Height/2.0-((kScreen_Width-64)/254.0*150.0+44)/2.0-20, kScreen_Width-64, 0);
self.showView.backgroundColor = [UIColor whiteColor];
self.showView.layer.cornerRadius = 20;
[self.mask2 addSubview:_showView];
// 標題列:用貝茲曲線做出上方兩角圓形
UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _showView.width, 31)];
titleLab.text = @"分享成功";
titleLab.textAlignment = NSTextAlignmentCenter;
titleLab.backgroundColor = [UIColor redColor];
titleLab.textColor = [UIColor whiteColor];
titleLab.font = [UIFont systemFontOfSize:15];
UIBezierPath *titlePath = [UIBezierPath bezierPathWithRoundedRect:titleLab.bounds
byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight
cornerRadii:CGSizeMake(20, 20)];
CAShapeLayer *titleLayer = [CAShapeLayer layer];
titleLayer.frame = titleLab.bounds;
titleLayer.path = titlePath.CGPath;
titleLab.layer.mask = titleLayer;
[_showView addSubview:titleLab];
// 推薦標題
UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(16, 31+16, _showView.width-32, 15)];
lab.textAlignment = NSTextAlignmentLeft;
lab.text = @"大家都在看";
lab.textColor = [UIColor colorWithHexColorString:@"000000"];
lab.font = [UIFont systemFontOfSize:15];
[_showView addSubview:lab];
// 動態產生推薦項目,並以分隔線隔開
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:@"", @"" nil];
int y = 31+16+15+16;
for (int i = 0; i < arr.count; i++) {
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
CGSize size = [self getStringSize:arr[i] andFont:13 andWidth:self.showView.width-32];
button1.tag = 600+i;
button1.frame = CGRectMake(16, y, _showView.width-32, size.height);
[button1 setTitle:arr[i] forState:UIControlStateNormal];
button1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[button1 setTitleColor:[UIColor colorWithHexColorString:@"0096ff"] forState:UIControlStateNormal];
button1.titleLabel.font = [UIFont systemFontOfSize:13];
button1.titleLabel.numberOfLines = 0;
[button1 addTarget:self action:@selector(button1Click:) forControlEvents:UIControlEventTouchUpInside];
[_showView addSubview:button1];
y += size.height + 16;
if (i+1 != arr.count) {
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(16, y, self.showView.width-32, 0.5)];
line.backgroundColor = [UIColor colorWithHexColorString:@"f0f0f0"];
[_showView addSubview:line];
y += 0.5 + 16;
}
}
// 依內容實際高度重算卡片高度,避免留白或截斷
self.showView.frame = CGRectMake(32, kScreen_Height/2.0-((kScreen_Width-64)/254.0*150.0+44)/2.0-20, kScreen_Width-64, y+16);
} else {
[MBProgressHUD showError:@"分享失敗"];
}
}
// 計算文字尺寸,供動態排版使用
-(CGSize)getStringSize:(NSString*)needString andFont:(CGFloat)font andWidth:(NSInteger)width {
return [needString boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]}
context:nil].size;
}
// 點擊落在卡片內不關閉,落在遮罩上才關閉
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isDescendantOfView:self.showView]) {
return NO;
}
return YES;
}
- (void)tap:(UITapGestureRecognizer *)sender {
[self.mask2 removeFromSuperview];
}
- (void)button1Click:(UIButton *)sender {
[self.mask2 removeFromSuperview];
switch (sender.tag) {
case 600: { } break;
case 601: { } break;
default: break;
}
}
手勢衝突:遮罩上的 tap 手勢會與卡片的點擊事件互搶,因此必須實作 gestureRecognizer:shouldReceiveTouch:,判斷觸控點是否在 showView 的子樹內,是的話就回傳 NO 讓事件繼續往下傳。
動態高度:推薦項目數量不固定時,先用 boundingRectWithSize: 量出每一列的實際高度,累加完再回頭設定卡片 frame,否則會出現空白或文字被裁掉的窘境。
記得回收:遮罩是加在 window 上的,若忘了 removeFromSuperview,會殘留一層看不到卻吃掉所有點擊的視圖,這是相當難查的 bug。
以上就是本文的全部內容,希望對大家的學習有所幫助。