如果能帮助到你,请你给一颗星,谢谢!(If this can help you, please give it a star, Thanks!)
实现应用内悬浮按钮和辅助工具条,可以增加/修改 Item 项,通过事件索引完成各种场景页面的跳转。
Using CocoaPods:
pod 'DYFAssistiveTouchView'
Or
pod 'DYFAssistiveTouchView', '~> 4.3.1'
- 实例化
// Lazy load
- (DYFAssistiveTouchView *)touchView {
if (!_touchView) {
_touchView = [[DYFAssistiveTouchView alloc] init];
_touchView.frame = CGRectMake(0, 0, 50, 50);
}
return _touchView;
}
- 设置属性
- 设置悬浮按钮的各种状态的图片
UIImage *leftHidenImage = [UIImage imageNamed:@"atv_hide_left"];
UIImage *rightHidenImage = leftHidenImage;
UIImage *leftNormalImage = [UIImage imageNamed:@"atv_normal_left"];
UIImage *rightNormalImage = leftNormalImage;
UIImage *leftHighlightedImage = [UIImage imageNamed:@"atv_normal_left"];
UIImage *rightHighlightedImage = leftHighlightedImage;
self.touchView.touchObject.leftNormalImage = leftNormalImage;
self.touchView.touchObject.rightNormalImage = rightNormalImage;
self.touchView.touchObject.leftHighlightedImage = leftHighlightedImage;
self.touchView.touchObject.rightHighlightedImage = rightHighlightedImage;
self.touchView.touchObject.leftTranslucentImage = leftHidenImage;
self.touchView.touchObject.rightTranslucentImage = rightHidenImage;
- 设置Unit对象
UIImage *leftUint1Image = [UIImage imageNamed:@"atv_unit1_left"];
UIImage *rightUint1Image = [UIImage imageNamed:@"atv_unit1_right"];
UIImage *leftUint2Image = [UIImage imageNamed:@"atv_unit2_left"];
UIImage *rightUint2Image = [UIImage imageNamed:@"atv_unit2_right"];
self.touchView.unitObject.leftTouchImage = leftUint1Image;
self.touchView.unitObject.rightTouchImage = rightUint1Image;
self.touchView.unitObject.leftItemBackgroundImage = leftUint2Image;
self.touchView.unitObject.rightItemBackgroundImage = rightUint2Image;
- 设置Item对象
UIImage *userImage = [UIImage imageNamed:@"atv_item_user"];
UIImage *cafeImage = [UIImage imageNamed:@"atv_item_cafe"];
UIImage *csImage = [UIImage imageNamed:@"atv_item_cs"];
DYFAssistiveTouchItem *item = [[DYFAssistiveTouchItem alloc] init];
item.image = userImage;
DYFAssistiveTouchItem *item1 = [[DYFAssistiveTouchItem alloc] init];
item1.image = cafeImage;
DYFAssistiveTouchItem *item2 = [[DYFAssistiveTouchItem alloc] init];
item2.image = csImage;
self.touchView.items = @[item, item1, item2];
- 是否显示
[self.touchView isShowing]
- 显示
[self.touchView show];
- 隐藏
[self.touchView hide];
- 隐藏一半至屏幕
[self.touchView setShouldShowHalf:YES];
- 设置初始显示位置
[self.touchView setTouchViewPlace:DYFTouchViewAtMiddleRight];
- 响应事件(二选一)
- (void)presentAtIndex:(NSInteger)index {
NSString *url = @"https://support.apple.com/zh-cn";
if (index == 0) {
url = @"https://github.com/itenfay";
}
else if (index == 1) {
url = @"https://github.com/itenfay/Awesome";
}
else {
url = @"https://www.jianshu.com/u/7fc76f1179cc";
}
SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:url]];
[self presentViewController:safariVC animated:YES completion:NULL];
}
- Block实现
- (IBAction)configureAction:(id)sender {
if (!_touchView) {
[self configTouchViewDotImages];
[self configTouchViewUnits];
[self configTouchViewItems];
[self.touchView setShouldShowHalf:YES];
[self.touchView setTouchViewPlace:DYFTouchViewAtMiddleRight];
__weak typeof(self) weakSelf = self;
[self.touchView touchViewItemDidClickedAtIndex:^(DYFAssistiveTouchView *touchView) {
NSInteger index = touchView.indexOfItem;
NSLog(@"Index of item: %zi", index);
[weakSelf presentAtIndex:index];
}];
}
}
- 代理实现
// 协议
Protocol: <DYFAssistiveTouchViewDelegate>
// 设置代理
self.touchView.delegate = self;
// 代理实现
- (void)touchViewItemDidClickedAtIndex:(DYFAssistiveTouchView *)touchView {
NSInteger index = touchView.indexOfItem;
NSLog(@"Index of item: %zi", index);
[self presentAtIndex:index];
}