Android 和 iOS LoadingView单例模式的实现


应用场景: 
在对用户或者流程上需要进行堵塞操作时候需要block,尤其是像Login, Oath 这种网络请求,
这时候就需要个loadingview来作为缓冲(对于些展示的界面的网络请求,没有必要block)。
对全局写个易用性好的LoadingView就很有必要,这个View相当于是个资源的Manager,基于这些写个单例是非常适合此种情况的。

上代码吧....

1.iOS


.h

+(id)getShareInstance;
-(void)show;
-(void)stop;


.m

+(id)getShareInstance{
    
    if(!instance){
        instance = [[JYTLoadingView alloc] init];
    }
    return instance;
}


- (id)initWithFrame:(CGRect)frame
{
    if (self) {
        self = [[[NSBundle mainBundle] loadNibNamed:@"JYTLoadingView" owner:self options:nil] objectAtIndex:0];
        
        CGRect frame = [UIScreen mainScreen].bounds;
        self.center = CGPointMake(frame.size.width/2, frame.size.height/2);
        self.layer.cornerRadius = 8.0;
    }
    return self;
}

-(void)show{
    [self.indicatorView startAnimating];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
     timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(stop) userInfo:nil repeats:NO];
    
}

-(void)stop{
    [self removeFromSuperview];
}


V2改进版,对getShareInstance进一步封装,对与外围试用者而言,只需要知道show和stop接口即可:

.h:

-(void)show;
-(void)stop;


.m

<p class="p1">+(<span style="font-family: Arial, Helvetica, sans-serif;">JYTLoadingView *</span><span style="font-family: Arial, Helvetica, sans-serif;">) getShareInstance{</span></p>if(!instance){
        instance = [[JYTLoadingView alloc] init];
    }
    return instance;
}

- (id)initWithFrame:(CGRect)frame
{
    if (self) {
        self = [[[NSBundle mainBundle] loadNibNamed:@"JYTLoadingView" owner:self options:nil] objectAtIndex:0];
        
        CGRect frame = [UIScreen mainScreen].bounds;
        self.center = CGPointMake(frame.size.width/2, frame.size.height/2);
        self.layer.cornerRadius = 8.0;
    }
    return self;
}

+(void)show{
    
    [[[JYTLoadingView getShareInstance] indicatorView] startAnimating];
    [[UIApplication sharedApplication].keyWindow addSubview: [JYTLoadingView getShareInstance]];
     timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(stop) userInfo:nil repeats:NO];
}

+(void)stop{
    [[self getShareInstance] removeFromSuperview];
}

V3 .m进行改进,对于代码alloc 的方式,view走 initWithFrame, 而对于通过 IB方式的设计的view, 是不会走这个生命周期中的方法

If you create a view object programmatically, this method is the designated initializer for theUIView class. Subclasses can override this method to perform any custom initialization but must callsuper at the beginning of their implementation.

If you use Interface Builder to design your interface, this method is not called when your view objects are subsequently loaded from the nib file.


+(LoadingScreen *)getShareInstance{
    if(!shareInstance){
        shareInstance = [[[NSBundle mainBundle] loadNibNamed:@"LoadingScreen" owner:self options:nil] objectAtIndex:0];
        [shareInstance mainLoadingView].center = shareInstance.center;
        [shareInstance loadingLable].text = NSLocalizedString(@"Indicator.Message.Loading", nil);
    }
    return shareInstance;
}

+(void)show{
    [[[LoadingScreen getShareInstance] indicatorView] startAnimating];
    [[UIApplication sharedApplication].keyWindow addSubview:[LoadingScreen getShareInstance]];
}

+(void)stop{
    [[[LoadingScreen getShareInstance] indicatorView] stopAnimating];
    [[LoadingScreen getShareInstance] removeFromSuperview];
}

在将LoadingView封装到Controller和UIVIew的基类里,这样外部view进行调用时更方便的说

新建的UIView Category:

@implementation UIView (Loading)

-(void)showLoadingScreen{
    [LoadingScreen show];
}

-(void)stopLoadingScreen{
    [LoadingScreen stop];
}

@end


2.Android TODO




      

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android实现加载过程动画可以使用ProgressBar或者自定义View实现。以下是两种实现方式: 1. 使用ProgressBar ProgressBar是Android系统自带的控件,可以实现加载过程动画。可以通过以下代码实现: ``` <ProgressBar android:id="@+id/progress_bar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:indeterminate="true" /> ``` 其中,android:indeterminate="true"表示ProgressBar是一个不确定进度的动画。 2. 自定义View 可以通过自定义View实现更加个性化的加载过程动画。以下是一个简单的示例代码: ``` public class LoadingView extends View { private Paint mPaint; private RectF mRectF; private float mStartAngle = 0; private float mSweepAngle = 45; private int mWidth; private int mHeight; public LoadingView(Context context) { super(context); init(); } public LoadingView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } public LoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mPaint = new Paint(); mPaint.setColor(Color.RED); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(10); mPaint.setAntiAlias(true); mRectF = new RectF(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mRectF.set(0, 0, mWidth, mHeight); canvas.drawArc(mRectF, mStartAngle, mSweepAngle, false, mPaint); mStartAngle += 5; if (mStartAngle >= 360) { mStartAngle = 0; } postInvalidateDelayed(10); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); mWidth = MeasureSpec.getSize(widthMeasureSpec); mHeight = MeasureSpec.getSize(heightMeasureSpec); } } ``` 该自定义View会绘制一个旋转的圆弧,可以通过改变mStartAngle和mSweepAngle的值来改变动画效果。在使用时,直接将该View加入布局即可: ``` <com.example.myapplication.LoadingView android:layout_width="100dp" android:layout_height="100dp" android:layout_centerInParent="true" /> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值