4

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

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

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

4

3 回答 3

2

我试着翻译这个。我不确定它会起作用,但它确实可以编译。请注意,链接到的页面包含表明 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);
                }
            }
        }
    }
}
于 2011-04-27T17:37:38.833 回答
0

将以下代码转换为 C#:http ://apptech.next-munich.com/2010/04/customizing-uipagecontrols-looks.html

于 2011-04-27T13:07:57.080 回答
0

我将这个这个结合起来用于单点触控。我希望这有帮助。

用法是这样的:

_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
}
于 2012-08-19T21:42:22.687 回答