Skip to content

Commit d7f3003

Browse files
author
YSC
committed
Update->Support Orientation
1 parent e3b9e9f commit d7f3003

File tree

1 file changed

+149
-82
lines changed

1 file changed

+149
-82
lines changed

Demo/WSProgressHUD/WSProgressHUD.m

Lines changed: 149 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ @interface WSProgressHUD ()
5252

5353
@property (nonatomic) BOOL isRotate;
5454

55+
@property (nonatomic) UIInterfaceOrientation currentDeviceOrientation;
56+
57+
5558
@end
5659

5760
static CGFloat stringWidth = 0.0f;
@@ -300,7 +303,7 @@ - (void)showWithMaskType: (WSProgressHUDMaskType)maskType maskWithout: (WSProgre
300303

301304
- (void)showWithString: (NSString *)string maskType: (WSProgressHUDMaskType)maskType maskWithout: (WSProgressHUDMaskWithoutType)withoutType
302305
{
303-
NSAssert([NSThread isMainThread], @"Show Must on main thread");
306+
NSAssert([NSThread isMainThread], @"WSProgressHUD show Must on main thread");
304307
objc_setAssociatedObject(self, @selector(maskType), @(maskType), OBJC_ASSOCIATION_ASSIGN);
305308
objc_setAssociatedObject(self, @selector(hudType), @(WSProgressHUDTypeStatus), OBJC_ASSOCIATION_ASSIGN);
306309
objc_setAssociatedObject(self, @selector(withoutType), @(withoutType), OBJC_ASSOCIATION_ASSIGN);
@@ -309,27 +312,30 @@ - (void)showWithString: (NSString *)string maskType: (WSProgressHUDMaskType)mask
309312

310313
[self setMaskEdgeWithType:self.maskType];
311314

312-
[self updateSubviewsPositionWithString:string];
315+
self.currentString = string;
316+
317+
[self updateSubviewsPosition];
313318

314319
[self showHudViewWithAnimation];
315320
}
316321

317322

318323
- (void)showShimmeringString: (NSString *)string maskType: (WSProgressHUDMaskType)maskType maskWithout: (WSProgressHUDMaskWithoutType)withoutType
319324
{
320-
NSAssert([NSThread isMainThread], @"Show Must on main thread");
325+
NSAssert([NSThread isMainThread], @"WSProgressHUD show Must on main thread");
321326
objc_setAssociatedObject(self, @selector(maskType), @(maskType), OBJC_ASSOCIATION_ASSIGN);
322327
objc_setAssociatedObject(self, @selector(hudType), @(WSProgressHUDTypeString), OBJC_ASSOCIATION_ASSIGN);
323328
objc_setAssociatedObject(self, @selector(withoutType), @(withoutType), OBJC_ASSOCIATION_ASSIGN);
324329

325330
[self invalidateTimer];
326331

327332
[self setMaskEdgeWithType:self.maskType];
328-
333+
334+
self.currentString = string;
329335
if (string) {
330336
objc_setAssociatedObject(self, @selector(onlyShowTitle), @(1), OBJC_ASSOCIATION_ASSIGN);
331337

332-
[self updateSubviewsPositionWithString:string];
338+
[self updateSubviewsPosition];
333339

334340
[self showHudViewWithAnimation];
335341

@@ -341,7 +347,7 @@ - (void)showShimmeringString: (NSString *)string maskType: (WSProgressHUDMaskTyp
341347

342348
- (void)showProgress:(CGFloat)progress status:(NSString*)string maskType:(WSProgressHUDMaskType)maskType maskWithout: (WSProgressHUDMaskWithoutType)withoutType
343349
{
344-
NSAssert([NSThread isMainThread], @"Show Must on main thread");
350+
NSAssert([NSThread isMainThread], @"WSProgressHUD show Must on main thread");
345351
if (self.hudIsShowing && self.hudType == WSProgressHUDTypeProgress) {
346352
self.ringLayer.strokeEnd = progress;
347353
return;
@@ -354,7 +360,9 @@ - (void)showProgress:(CGFloat)progress status:(NSString*)string maskType:(WSProg
354360

355361
[self setMaskEdgeWithType:self.maskType];
356362

357-
[self updateSubviewsPositionWithString:string];
363+
self.currentString = string;
364+
365+
[self updateSubviewsPosition];
358366

359367
[self showHudViewWithAnimation];
360368

@@ -363,7 +371,7 @@ - (void)showProgress:(CGFloat)progress status:(NSString*)string maskType:(WSProg
363371

364372
- (void)showImage:(UIImage *)image status:(NSString *)title maskType: (WSProgressHUDMaskType)maskType maskWithout: (WSProgressHUDMaskWithoutType)withoutType
365373
{
366-
NSAssert([NSThread isMainThread], @"Show Must on main thread");
374+
NSAssert([NSThread isMainThread], @"WSProgressHUD show Must on main thread");
367375
objc_setAssociatedObject(self, @selector(maskType), @(maskType), OBJC_ASSOCIATION_ASSIGN);
368376
objc_setAssociatedObject(self, @selector(showImage), @(1), OBJC_ASSOCIATION_ASSIGN);
369377
objc_setAssociatedObject(self, @selector(hudType), @(WSProgressHUDTypeImage), OBJC_ASSOCIATION_ASSIGN);
@@ -373,7 +381,9 @@ - (void)showImage:(UIImage *)image status:(NSString *)title maskType: (WSProgres
373381

374382
[self setMaskEdgeWithType:self.maskType];
375383

376-
[self updateSubviewsPositionWithString:title];
384+
self.currentString = title;
385+
386+
[self updateSubviewsPosition];
377387

378388
[self showHudViewWithAnimation];
379389

@@ -509,84 +519,97 @@ - (void)addOverlayViewToWindow
509519
}
510520

511521

512-
- (void)updateSubviewsPositionWithString: (NSString *)string
522+
- (void)updateSubviewsPosition
513523
{
514-
CGSize hudSize = [self hudSizeWithString:string];
524+
NSString *string = self.currentString;
515525

516-
CGFloat centerX = self.frame.size.width / 2;
517-
CGFloat centerY = self.frame.size.height / 2 - 20;
526+
CGSize hudSize = [self hudSizeWithString:string];
518527
CGRect hudBounds = CGRectMake(0, 0, hudSize.width, hudSize.height);
528+
519529
if (self.hudIsShowing) {
520-
WSProgressHUDNewBounds = hudBounds;
521-
[self stopIndicatorAnimation];
530+
if (self.isRotate) {
531+
[self updatePositionWithString:string hudBounds:hudBounds];
532+
self.isRotate = NO;
533+
} else {
534+
WSProgressHUDNewBounds = hudBounds;
535+
[self stopIndicatorAnimation];
536+
}
522537
} else {
523-
self.hudView.bounds = hudBounds;
524-
self.labelView.frame = WSProgressHUDStringRect;//Reset the view frame
525-
self.hudView.center = CGPointMake(centerX, centerY);
526-
527-
CGFloat hudCenterX = CGRectGetWidth(hudBounds)/2;
528-
CGFloat hudCenterY = CGRectGetHeight(hudBounds)/2;
529-
530-
switch (self.hudType) {
531-
case WSProgressHUDTypeStatus: {
532-
533-
[self startIndicatorAnimation:YES];
534-
535-
if (string) {
536-
self.labelView.frame = WSProgressHUDStringRect;
537-
self.labelView.center = CGPointMake(hudCenterX + 10, hudCenterY);
538-
self.spinnerView.center = self.indefiniteAnimationView.center = self.indicatorView.center = CGPointMake(15, hudCenterY);
539-
540-
} else {
541-
self.spinnerView.center = self.indefiniteAnimationView.center = self.indicatorView.center = CGPointMake(hudCenterX, hudCenterY);
542-
}
543-
544-
}break;
545-
546-
case WSProgressHUDTypeString: {
538+
[self updatePositionWithString:string hudBounds:hudBounds];
539+
}
540+
541+
542+
}
543+
544+
- (void)updatePositionWithString: (NSString *)string hudBounds: (CGRect)bounds
545+
{
546+
CGFloat centerX = self.bounds.size.width / 2;
547+
CGFloat centerY = self.bounds.size.height / 2 - 20;
548+
549+
self.hudView.bounds = bounds;
550+
self.labelView.frame = WSProgressHUDStringRect;//Reset the view frame
551+
self.hudView.center = CGPointMake(centerX, centerY);
552+
553+
CGFloat hudCenterX = CGRectGetWidth(bounds)/2;
554+
CGFloat hudCenterY = CGRectGetHeight(bounds)/2;
555+
556+
switch (self.hudType) {
557+
case WSProgressHUDTypeStatus: {
558+
559+
[self startIndicatorAnimation:YES];
560+
561+
if (string) {
547562
self.labelView.frame = WSProgressHUDStringRect;
548-
549563
self.labelView.center = CGPointMake(hudCenterX + 10, hudCenterY);
564+
self.spinnerView.center = self.indefiniteAnimationView.center = self.indicatorView.center = CGPointMake(15, hudCenterY);
550565

551-
self.shimmeringView.frame = WSProgressHUDStringRect;
552-
[self setShimmeringLabelSize:WSProgressHUDStringRect.size];
553-
554-
self.shimmeringView.center = CGPointMake(hudCenterX, hudCenterY);
566+
} else {
567+
self.spinnerView.center = self.indefiniteAnimationView.center = self.indicatorView.center = CGPointMake(hudCenterX, hudCenterY);
568+
}
569+
570+
}break;
571+
572+
case WSProgressHUDTypeString: {
573+
self.labelView.frame = WSProgressHUDStringRect;
574+
575+
self.labelView.center = CGPointMake(hudCenterX + 10, hudCenterY);
576+
577+
self.shimmeringView.frame = WSProgressHUDStringRect;
578+
[self setShimmeringLabelSize:WSProgressHUDStringRect.size];
579+
580+
self.shimmeringView.center = CGPointMake(hudCenterX, hudCenterY);
581+
[self startIndicatorAnimation:NO];
582+
}break;
583+
584+
case WSProgressHUDTypeProgress: {
585+
if (string) {
586+
self.ringLayer.position = self.backgroundRingLayer.position = CGPointMake(15, hudCenterY);
587+
self.labelView.frame = WSProgressHUDStringRect;
588+
self.labelView.center = CGPointMake(hudCenterX + 10, hudCenterY);
589+
} else {
590+
self.ringLayer.position = self.backgroundRingLayer.position = CGPointMake(hudCenterX, hudCenterY);
591+
}
592+
593+
}break;
594+
case WSProgressHUDTypeImage: {
595+
596+
if (self.imageView.image) {
597+
WSProgressHUDStringRect.origin.y = imageOffset;
555598
[self startIndicatorAnimation:NO];
556-
}break;
557-
558-
case WSProgressHUDTypeProgress: {
559-
if (string) {
560-
self.ringLayer.position = self.backgroundRingLayer.position = CGPointMake(15, hudCenterY);
561-
self.labelView.frame = WSProgressHUDStringRect;
562-
self.labelView.center = CGPointMake(hudCenterX + 10, hudCenterY);
563-
} else {
564-
self.ringLayer.position = self.backgroundRingLayer.position = CGPointMake(hudCenterX, hudCenterY);
565-
}
566-
567-
}break;
568-
case WSProgressHUDTypeImage: {
569-
570-
if (self.imageView.image) {
571-
WSProgressHUDStringRect.origin.y = imageOffset;
572-
[self startIndicatorAnimation:NO];
573-
self.labelView.center = CGPointMake(hudCenterX , hudCenterY + 20);
574-
self.labelView.textAlignment = NSTextAlignmentCenter;
575-
self.imageView.center = CGPointMake(hudCenterX, 30);
576-
} else {
577-
self.labelView.text = string;
578-
self.labelView.center = CGPointMake(hudCenterX, hudCenterY);
579-
}
580-
581-
}break;
582-
583-
default:
584-
break;
585-
}
586-
599+
self.labelView.center = CGPointMake(hudCenterX , hudCenterY + 20);
600+
self.labelView.textAlignment = NSTextAlignmentCenter;
601+
self.imageView.center = CGPointMake(hudCenterX, 30);
602+
} else {
603+
self.labelView.text = string;
604+
self.labelView.center = CGPointMake(hudCenterX, hudCenterY);
605+
}
606+
607+
}break;
608+
609+
default:
610+
break;
587611
}
588612
}
589-
590613
- (CGFloat)valueByScreenScale: (CGFloat)value
591614
{
592615
return ([UIScreen mainScreen].bounds.size.width / 320 * value);
@@ -848,6 +871,7 @@ - (void)setShimmeringLabelSize: (CGSize)size
848871

849872
- (void)setMaskEdgeWithType: (WSProgressHUDMaskType)maskType
850873
{
874+
851875
if (maskType != WSProgressHUDMaskTypeDefault) {
852876
switch (self.withoutType) {
853877
case WSProgressHUDMaskWithoutDefault: {
@@ -857,15 +881,15 @@ - (void)setMaskEdgeWithType: (WSProgressHUDMaskType)maskType
857881
}break;
858882
case WSProgressHUDMaskWithoutNavigation: {
859883
maskBottomEdge = 0;
860-
maskTopEdge = 64;
884+
maskTopEdge = [self maskTopEdge];
861885
}break;
862886
case WSProgressHUDMaskWithoutTabbar: {
863-
maskBottomEdge = 0;
864-
maskTopEdge = 64;
887+
maskBottomEdge = 49;
888+
maskTopEdge = 0;
865889
}break;
866890
case WSProgressHUDMaskWithoutNavAndTabbar: {
867891
maskBottomEdge = 49;
868-
maskTopEdge = 64;
892+
maskTopEdge = [self maskTopEdge];
869893
}break;
870894

871895
default:
@@ -878,9 +902,9 @@ - (void)setMaskEdgeWithType: (WSProgressHUDMaskType)maskType
878902

879903
self.overlayView.frame = CGRectMake(0, maskTopEdge, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - maskTopEdge - maskBottomEdge);
880904
if (self.showOnTheWindow) {
881-
CGRect rect = self.frame;
905+
CGRect rect = self.bounds;
882906
rect.size = self.overlayView.frame.size;
883-
self.frame = rect;
907+
self.bounds = rect;
884908
}
885909
}
886910

@@ -971,6 +995,15 @@ - (CAShapeLayer *)createRingLayerWithCenter:(CGPoint)center radius:(CGFloat)radi
971995
return slice;
972996
}
973997

998+
- (CGFloat)maskTopEdge
999+
{
1000+
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
1001+
return 32;
1002+
} else {
1003+
return 64;
1004+
}
1005+
}
1006+
9741007

9751008
#pragma mark - Custom
9761009
+ (void)setProgressHUDIndicatorStyle: (WSProgressHUDIndicatorStyle)style {
@@ -1027,6 +1060,31 @@ - (void)drawRect:(CGRect)rect
10271060
}
10281061
}
10291062

1063+
#pragma mark - Observe
1064+
1065+
- (void)registerOrientationDidChangeObserve
1066+
{
1067+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationDidChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
1068+
1069+
}
1070+
1071+
- (void)statusBarOrientationDidChange: (NSNotification *)notification {
1072+
if (self.superview) {
1073+
self.bounds = self.superview.bounds;
1074+
1075+
if (self.hudIsShowing) {
1076+
//更新Frame
1077+
self.isRotate = YES;
1078+
1079+
[self setMaskEdgeWithType:self.maskType];
1080+
1081+
[self updateSubviewsPosition];
1082+
1083+
[self setNeedsDisplay];
1084+
}
1085+
}
1086+
1087+
}
10301088

10311089
#pragma mark - Init View
10321090
- (instancetype)initWithView:(UIView *)view {
@@ -1065,6 +1123,10 @@ - (instancetype)initWithFrame:(CGRect)frame
10651123
self.backgroundColor = [UIColor clearColor];
10661124

10671125
self.userInteractionEnabled = NO;
1126+
1127+
self.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
1128+
1129+
[self registerOrientationDidChangeObserve];
10681130
}
10691131
return self;
10701132
}
@@ -1219,5 +1281,10 @@ - (BOOL)showOnTheWindow
12191281
}
12201282

12211283

1284+
- (void)dealloc
1285+
{
1286+
[[NSNotificationCenter defaultCenter] removeObserver:self];
1287+
}
1288+
12221289

12231290
@end

0 commit comments

Comments
 (0)