更改 MonoTouch 中 UIPageControl 点的颜色

发布于 2024-11-03 14:31:02 字数 193 浏览 3 评论 0原文

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

我知道没有可用的公共 API,但我想知道 MonoTouch 中是否内部实现了任何东西来改进这一点。

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

I was wondering if MonoTouch allows the developer to change the colour of UIPageControl dots to suit a light background - they seem to be fixed white, which makes them very hard to see on a light textured background.

I am aware there is no public API available for this but I was wondering if anything was internally implemented in MonoTouch to improve on this.

Otherwise, what's the recommended approach to using a UIPageControl on a light background?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤者何惧 2024-11-10 14:31:02

我尝试翻译一下这个。我不确定它会起作用,但它确实可以编译。请注意,链接到的页面包含注释,表明 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);
                }
            }
        }
    }
}

I took a stab at translating this. I'm not sure it will work, but it does compile. Note that the page linked to contains comments indicating that Apple frowns upon this code and may reject it:

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);
                }
            }
        }
    }
}
爱情眠于流年 2024-11-10 14:31:02

我结合了这个和<一href="https://stackoverflow.com/questions/2942636/how-i-change-the-color-of-pagination-dots-of-uipagecontrol/7677213#7677213">此用于单点触控。我希望这有帮助。

用法是这样的:

_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
}

I combined this and this for monotouch. I hope this helps.

The usage is like this:

_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);
};

And the class like this.

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文