由于项目需求需要自我定制分享面板,所以自己写了个分享sheet,此控件就是对UIView的一个简单的封装,中间遇到了一个坑,做此记录,以巩固基础知识
只展示核心代码
.h代码
#import "BLBaseViewController.h"
typedef void(^platformClick)(int platform);
@interface CYShareSheetView : UIView
- (void)platformClicked:(platformClick)platform;
- (void)show;
@end
外部只需要暴露两个方法,第一个用来传分享platform类型,第二个就是用来展示分享界面的
.m部分代码
- (instancetype)init
{
self = [super init];
if (self) {
UIWindow *window = [[UIApplication sharedApplication]keyWindow];
self.frame = CGRectMake(0, 0 , mScreenWidth, mScreenHeight);
[window addSubview:self];
self.hidden = YES;
[self setUpUI];
}
return self;
}
- (void)cancelBtnClick:(UIButton *)btn
{
[self dismiss];
}
- (void)buttonClick:(UIButton *)btn
{
int index = ((int)(btn.tag)-1000);
int platform = [self platformName:index];
if (self.platform) {
self.platform(platform);
}
[self dismiss];
}
- (void)platformClicked:(platformClick)platformClicked
{
self.platform = platformClicked;
}
- (void)tap:(UITapGestureRecognizer *)tap
{
[self dismiss];
}
- (void)show
{
UIWindow *window = [[UIApplication sharedApplication]keyWindow];
[window addSubview:self];
[UIView animateWithDuration:0.3 animations:^{
// 设置整个view弹出来以后的位置(最终位置)
self.shadeView.alpha = 0.5f;
self.hidden = NO;
self.backgroundView.frame = CGRectMake(0, self.bounds.size.height-bgHeight, self.bounds.size.width, bgHeight);
}completion:^(BOOL finished) {
self.shadeView.alpha = 1.0f;
}];
}
- (void)dismiss
{
[UIView animateWithDuration:0.3f animations:^{
//整个View的初始位置,
self.shadeView.alpha = 0.1f;
self.backgroundView.frame = CGRectMake(0, self.bounds.size.height, self.bounds.size.width, bgHeight);
} completion:^(BOOL finished) {
self.shadeView.alpha = 0.f;
self.hidden = YES;
[self removeFromSuperview];
}];
}
#pragma mark-
#pragma mark -- share
- (int)platformName:(int)index
{
if (index == 0) {
return UMSocialPlatformType_WechatSession;
}else if (index == 1) {
return UMSocialPlatformType_WechatTimeLine;
}else {
return UMSocialPlatformType_Sina;
}
}
首先是初始化方法,这里需要着重注意的就是,控件里的frame初始化位置一定要设置对,这样之后show 和dismiss 对应的frame设置好后,动画效果也就能达到我们想要的效果,(我之前就是UI初始化位置不对导致控件动画错乱)
然后着重要巩固的就是 animateWithDuration这个方法,
duration为动画持续的时间。
animations为动画效果的代码块。
-
下面是可以设置动画效果的属性:
frame
bounds
center
transform
alpha
backgroundColor
-
contentStretch
completion为动画执行完毕以后执行的代码块
options为动画执行的选项。可以参考这里
-
delay为动画开始执行前等待的时间
如何实现连续的动画?
可以在completion代码块中添加动画。当然了,分享面板这里明显不需要连续的动画,而是分开的。这里只需要用到frame(移动动画特效) 和 alpha (淡入淡出)特效就够了
再次强调一遍,在写动画特效之前,一定要在init里初始化好控件对应的坐标,
backgroundView就是分享版面,上面则是一个背景为半透明的view,添加了点击手势
本文部分摘自 http://blog.csdn.net/jasonhejunyi/article/details/8504590
另附上我自己写的Demo,Demo中用的是友盟的sdk
https://github.com/chenbtxcy55/CYShareSheetView