public override void Draw(CGRect rect) { using (var context = UIGraphics.GetCurrentContext()) { switch (_maskType) { case MaskType.Black: UIColor.FromWhiteAlpha(0f, 0.5f).SetColor(); context.FillRect(Bounds); break; case MaskType.Gradient: nfloat[] colors = new nfloat[] { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.75f }; nfloat[] locations = new nfloat[] { 0.0f, 1.0f }; using (var colorSpace = CGColorSpace.CreateDeviceRGB()) { using (var gradient = new CGGradient(colorSpace, colors, locations)) { var center = new CGPoint(Bounds.Size.Width / 2, Bounds.Size.Height / 2); float radius = Math.Min((float)Bounds.Size.Width, (float)Bounds.Size.Height); context.DrawRadialGradient(gradient, center, 0, center, radius, CGGradientDrawingOptions.DrawsAfterEndLocation); } } break; } } }
public ProgressHUD(CGRect frame) : base(frame) { UserInteractionEnabled = false; BackgroundColor = UIColor.Clear; Alpha = 0; AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight; SetOSSpecificLookAndFeel(); }
void PositionHUD(NSNotification notification) { nfloat keyboardHeight = 0; double animationDuration = 0; UIInterfaceOrientation orientation = UIApplication.SharedApplication.StatusBarOrientation; bool ignoreOrientation = UIDevice.CurrentDevice.CheckSystemVersion(8, 0); if (notification != null) { var keyboardFrame = UIKeyboard.FrameEndFromNotification(notification); animationDuration = UIKeyboard.AnimationDurationFromNotification(notification); if (notification.Name == UIKeyboard.WillShowNotification || notification.Name == UIKeyboard.DidShowNotification) { if (ignoreOrientation || IsPortrait(orientation)) { keyboardHeight = keyboardFrame.Size.Height; } else { keyboardHeight = keyboardFrame.Size.Width; } } else { keyboardHeight = 0; } } else { keyboardHeight = VisibleKeyboardHeight; } CGRect orientationFrame = this.Window.Bounds; CGRect statusBarFrame = UIApplication.SharedApplication.StatusBarFrame; if (!ignoreOrientation && IsLandscape(orientation)) { orientationFrame.Size = new CGSize(orientationFrame.Size.Height, orientationFrame.Size.Width); statusBarFrame.Size = new CGSize(statusBarFrame.Size.Height, statusBarFrame.Size.Width); } var activeHeight = orientationFrame.Size.Height; if (keyboardHeight > 0) { activeHeight += statusBarFrame.Size.Height * 2; } activeHeight -= keyboardHeight; nfloat posY = (float)Math.Floor(activeHeight * 0.45); nfloat posX = orientationFrame.Size.Width / 2; nfloat textHeight = _stringLabel.Frame.Height / 2 + 40; switch (toastPosition) { case ToastPosition.Bottom: posY = activeHeight - textHeight; break; case ToastPosition.Center: // Already set above break; case ToastPosition.Top: posY = textHeight; break; default: break; } CGPoint newCenter; float rotateAngle; if (ignoreOrientation) { rotateAngle = 0.0f; newCenter = new CGPoint(posX, posY); } else { switch (orientation) { case UIInterfaceOrientation.PortraitUpsideDown: rotateAngle = (float)Math.PI; newCenter = new CGPoint(posX, orientationFrame.Size.Height - posY); break; case UIInterfaceOrientation.LandscapeLeft: rotateAngle = (float)(-Math.PI / 2.0f); newCenter = new CGPoint(posY, posX); break; case UIInterfaceOrientation.LandscapeRight: rotateAngle = (float)(Math.PI / 2.0f); newCenter = new CGPoint(orientationFrame.Size.Height - posY, posX); break; default: // as UIInterfaceOrientationPortrait rotateAngle = 0.0f; newCenter = new CGPoint(posX, posY); break; } } if (notification != null) { UIView.Animate(animationDuration, 0, UIViewAnimationOptions.AllowUserInteraction, delegate { MoveToPoint(newCenter, rotateAngle); }, null); } else { MoveToPoint(newCenter, rotateAngle); } }
void UpdatePosition(bool textOnly = false) { nfloat hudWidth = 100f; nfloat hudHeight = 100f; nfloat stringWidth = 0f; nfloat stringHeight = 0f; nfloat cancelHeight = 0f; nfloat stringHeightBuffer = 20f; nfloat stringAndImageHeightBuffer = 80f; /*if (IsiOS7) * { * hudHeight += 50; * hudWidth += 50f; * stringHeightBuffer += 50f; * stringAndImageHeightBuffer += 50f; * }*/ CGRect labelRect = new CGRect(); string @string = StringLabel.Text; // False if it's text-only bool imageUsed = (ImageView.Image != null) || (ImageView.Hidden); if (textOnly) { imageUsed = false; } if (imageUsed) { hudHeight = stringAndImageHeightBuffer + stringHeight; } else { hudHeight = (textOnly ? stringHeightBuffer : stringHeightBuffer + 40); } if (!string.IsNullOrEmpty(@string)) { int lineCount = Math.Min(10, @string.Split('\n').Length + 1); var stringSize = new NSString(@string).StringSize(StringLabel.Font, new CGSize(200, 30 * lineCount)); stringWidth = stringSize.Width; stringHeight = stringSize.Height; hudHeight += stringHeight; if (stringWidth > hudWidth) { hudWidth = (float)Math.Ceiling(stringWidth / 2) * 2; } float labelRectY = imageUsed ? 66 : 9; if (hudHeight > 100) { labelRect = new CGRect(12, labelRectY, hudWidth, stringHeight); hudWidth += 24; } else { hudWidth += 24; labelRect = new CGRect(0, labelRectY, hudWidth, stringHeight); } } // Adjust for Cancel Button var cancelRect = new CGRect(); string @cancelCaption = _cancelHud == null ? null : CancelHudButton.Title(UIControlState.Normal); if (!string.IsNullOrEmpty(@cancelCaption)) { const int gap = 20; int lineCount = Math.Min(10, @string.Split('\n').Length + 1); var cancelSize = new NSString(@cancelCaption).StringSize(StringLabel.Font, new CGSize(200, 30 * lineCount)); cancelHeight = cancelSize.Height; if (stringWidth > hudWidth) { hudWidth = (float)Math.Ceiling(stringWidth / 2) * 2; } // Adjust for label nfloat cancelRectY = 0f; if (labelRect.Height > 0) { cancelRectY = labelRect.Y + labelRect.Height + (nfloat)gap; } else { cancelRectY = (imageUsed ? 66 : 9); } if (hudHeight > 100) { cancelRect = new CGRect(12, cancelRectY, hudWidth, cancelHeight); labelRect = new CGRect(12, labelRect.Y, hudWidth, stringHeight); hudWidth += 24; } else { hudWidth += 24; cancelRect = new CGRect(0, cancelRectY, hudWidth, cancelHeight); labelRect = new CGRect(0, labelRect.Y, hudWidth, stringHeight); } CancelHudButton.Frame = cancelRect; hudHeight += (cancelRect.Height + gap); } HudView.Bounds = new CGRect(0, 0, hudWidth, hudHeight); if (!string.IsNullOrEmpty(@string)) { ImageView.Center = new CGPoint(HudView.Bounds.Width / 2, 36); } else { ImageView.Center = new CGPoint(HudView.Bounds.Width / 2, HudView.Bounds.Height / 2); } StringLabel.Hidden = false; StringLabel.Frame = labelRect; if (!textOnly) { if (!string.IsNullOrEmpty(@string)) { SpinnerView.Center = new CGPoint((float)Math.Ceiling(HudView.Bounds.Width / 2.0f) + 0.5f, 40.5f); if (_progress != -1) { BackgroundRingLayer.Position = RingLayer.Position = new CGPoint(HudView.Bounds.Width / 2, 36f); } } else { SpinnerView.Center = new CGPoint((float)Math.Ceiling(HudView.Bounds.Width / 2.0f) + 0.5f, (float)Math.Ceiling(HudView.Bounds.Height / 2.0f) + 0.5f); if (_progress != -1) { BackgroundRingLayer.Position = RingLayer.Position = new CGPoint(HudView.Bounds.Width / 2, HudView.Bounds.Height / 2.0f + 0.5f); } } } }