【问题标题】:Changing the colour of UIPageControl dots in MonoTouch在 MonoTouch 中更改 UIPageControl 点的颜色
【发布时间】:2011-04-27 11:50:06
【问题描述】:

我想知道 MonoTouch 是否允许开发人员更改 UIPageControl 点的颜色以适应浅色背景 - 它们似乎固定为白色,这使得它们在浅色纹理背景上很难看到。

我知道没有可用的公共 API,但我想知道 MonoTouch 是否在内部实现了任何改进。

否则,在浅色背景上使用 UIPageControl 的推荐方法是什么?

【问题讨论】:

  • 错了。这个问题是 MonoTouch 特有的。根本不是重复的。
  • 首先,MonoTouch 在 iOS 平台上不能做任何 Objective C 做不到的事情。其次,链接的答案告诉你它可以做到,以及如何做到。只需将代码转换为 C#(如果这是问题所在,您应该编辑您的问题,说明问题是将以下 Obj C 代码转换为 C#:apptech.next-munich.com/2010/04/…

标签: ios xamarin.ios uipagecontrol


【解决方案1】:

我尝试翻译这个。我不确定它会起作用,但它确实可以编译。请注意,链接到的页面包含 cmet,表明 Apple 不赞成此代码并可能会拒绝它:

using System;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace Whatever
{
    public class StyledPageControl : UIPageControl
    {
        public StyledPageControl () : base()
        {
        }

        public override int CurrentPage {
            get {
                return base.CurrentPage;
            }
            set {
                base.CurrentPage = value;
                string imgActive = NSBundle.MainBundle.PathForResource("activeImage", "png");
                string imgInactive = NSBundle.MainBundle.PathForResource("inactiveImage", "png");
                for (int subviewIndex = 0; subviewIndex < this.Subviews.Length; subviewIndex++)
                {
                    UIImageView subview = this.Subviews[subviewIndex] as UIImageView;
                    if (subviewIndex == value) 
                        subview.Image = UIImage.FromFile(imgActive);
                    else
                        subview.Image = UIImage.FromFile(imgInactive);
                }
            }
        }

        public override int Pages {
            get {
                return base.Pages;
            }
            set {
                base.Pages = value;
                string img = NSBundle.MainBundle.PathForResource("inactiveImage", "png");
                for (int subviewIndex = 0; subviewIndex < this.Subviews.Length; subviewIndex++)
                {
                    UIImageView subview = this.Subviews[subviewIndex] as UIImageView;
                        subview.Image = UIImage.FromFile(img);
                }
            }
        }
    }
}

【讨论】:

    【解决方案2】:
    【解决方案3】:

    我将thisthis 组合在一起用于单点触控。我希望这会有所帮助。

    用法是这样的:

    _pager.Change += delegate(object sender, EventArgs e) {
    var pc =  sender as PageControl;
    Console.WriteLine ("Change Delegate== " + pc.currentPage);
    
    var toPage = pc.currentPage;
    var pageOffset = _scroll.Frame.Width*toPage;
    PointF p = new PointF(pageOffset, 0);
    Console.WriteLine (pageOffset);
    _scroll.SetContentOffset(p,true);
    };
    

    还有这样的课。

    public class PageControl:UIView
    {
        #region ctor
        public PageControl (RectangleF rect) :base(rect)
        {
            this.BackgroundColor = UIColor.Red;
            this.CurrenColor=new CGColor(.2f,15f,10F);
            this.OtherColor =new CGColor(.77F,.71F,.60F);
        }
        #endregion 
    
        #region Fields  
        float kDotDiameter= 7.0f;
        float kDotSpacer = 7.0f;
    
        int _currentPage;
        int _numberOfPages;
        CGColor CurrenColor{get;set;}
        CGColor OtherColor{get;set;}    
    
        public int currentPage
        {
            set
            {
                _currentPage = Math.Min(Math.Max(0, value),_numberOfPages-1);
                this.SetNeedsDisplay();
            }
            get{return _currentPage;}
        }
    
        public int numberOfPages
        {
            set
            {
                _numberOfPages = Math.Max(0,value);
                _currentPage = Math.Min(Math.Max(0, _currentPage), _numberOfPages-1);
                this.SetNeedsDisplay();
            }
    
            get{return _numberOfPages;}
        }
        #endregion 
    
    
    
        #region Overrides
        public override void Draw (RectangleF rect)
        {
            base.Draw (rect);
            CGContext context = UIGraphics.GetCurrentContext();
            context.SetAllowsAntialiasing(true);
            RectangleF currentBounds = this.Bounds;
    
            float dotsWidth = this.numberOfPages*kDotDiameter + Math.Max(0,this.numberOfPages-1)*kDotSpacer;
            float x = currentBounds.GetMidX() - dotsWidth/2;
            float y = currentBounds.GetMidY() - kDotDiameter/2;
    
            for (int i = 0; i < _numberOfPages; i++) {
                RectangleF circleRect = new RectangleF(x,y,kDotDiameter,kDotDiameter);
                if (i==_currentPage) {
                    context.SetFillColor(this.CurrenColor);
                }
                else {
                    context.SetFillColor(this.OtherColor);
    
                }
                context.FillEllipseInRect(circleRect);
                x += kDotDiameter + kDotSpacer;
            }
    
        }
    
        public override void TouchesBegan (MonoTouch.Foundation.NSSet touches, UIEvent evt)
        {
            base.TouchesBegan (touches, evt);
            PointF touchpoint = (touches.AnyObject as MonoTouch.UIKit.UITouch).LocationInView(this);
            RectangleF currentbounds = this.Bounds;
            float x = touchpoint.X- currentbounds.GetMidX();
            if (x<0 && this.currentPage>=0) {
                this.currentPage--;
                Change(this,EventArgs.Empty);
            }
            else if (x>0 && this.currentPage<this.numberOfPages-1) {
                this.currentPage++;
                Change(this,EventArgs.Empty);
    
            }
    
        }
        #endregion
    
        #region delegate
        public event EventHandler Change;
    
        #endregion
    }
    

    【讨论】:

      猜你喜欢
      • 2011-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      相关资源
      最近更新 更多