2011-04-27 52 views
4

我在想,如果MonoTouch允許開發人員更改UIPageControl點的顏色以適應淺色背景 - 它們似乎是固定的白色,這使得它們很難在輕質紋理上看到背景。在MonoTouch中更改UIPageControl點的顏色

我知道沒有公共API可用於此,但我想知道是否在MonoTouch內部實現了任何內容來改善這一點。

否則,在淺色背景上使用UIPageControl的建議方法是什麼?

+0

重複:http://stackoverflow.com/questions/2673827/how-to-change-uipagecontrol-dots – Simeon 2011-04-27 12:21:14

+0

錯誤。這個問題是特定於MonoTouch的。根本不重複。 – tomfanning 2011-04-27 12:41:19

+0

首先,MonoTouch無法在iOS平臺上做任何事情,而Objective-C無法做到這一點。其次,鏈接的答案顯示它可以完成,以及如何完成。只需將代碼轉換爲C#(如果存在這個問題,您應該編輯您的問題,指出問題是將以下Obj C代碼轉換爲C#:http://apptech.next-munich.com/2010/04/customizing -uipagecontrols-looks.html) – Simeon 2011-04-27 13:06:58

回答

2

我花了刺傷翻譯這一點。我不確定它會起作用,但它確實可以編譯。需要注意的是鏈接到網頁包含註釋,表明在此代碼蘋果皺眉並可能拒絕:

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

我結合對MonoTouch的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 
}