monotouch在ipad中的实例应用--显示图像和文字
本节主要讲述在苹果环境中使用monodevelop开发ipad的一个实例,具体操作如下
1、新建项目 选择monotouch--ipad--Empty project 命名为iPad01
2、添加新文件,选择monotouch--ipad view,命名为showView,这样会在项目中多出来三个文件。
3、在AppDelegate.cs中添加如下代码,以在运行后,初始化运行界面为ShowView;
1 UIWindow window; 2 showViews shows; 3 4 public override bool FinishedLaunching(UIApplication app, NSDictionary options) { 5 // create a new window instance based on the screen size 6 window = new UIWindow(UIScreen.MainScreen.Bounds); 7 shows = new showViews(); 8 window.RootViewController = shows; 9 // If you have defined a view, add it here: 10 // window.AddSubview (navigationController.View); 11 12 // make the window visible 13 window.MakeKeyAndVisible(); 14 15 return true; 16 }
4、添加新类MyViewController.cs,此类继承UIViewController,重写ipad的自动旋转行为,代码如下:
using MonoTouch.UIKit; namespace ipad01 { public class MyViewController:UIViewController { public MyViewController() { } public override bool ShouldAutorotateToInterfaceOrientation(UIInterfaceOrientation toInterfaceOrientation) { return true; } } }
5、在showViews.cs中添加如下代码
1 public override void ViewDidLoad() { 2 base.ViewDidLoad(); 3 4 var text = new UITextView(); 5 text.Text = "gwtGolden Gate Bridge,\nSan Francisco"; 6 text.Font = UIFont.FromName("Helvetica", 36f); 7 text.Frame = new System.Drawing.RectangleF(200, 40, 400, 100); 8 text.AutoresizingMask = UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleTopMargin | 9 UIViewAutoresizing.FlexibleRightMargin | UIViewAutoresizing.FlexibleBottomMargin; 10 text.ScrollEnabled = false; 11 text.Editable = false; 12 UIImage image = UIImage.FromFile("GoldenGate.jpg"); 13 UIImageView imageView = new UIImageView(image); 14 var customScrollView = new UIScrollView(this.View.Bounds); 15 customScrollView.AutoresizingMask = UIViewAutoresizing.FlexibleHeight 16 | UIViewAutoresizing.FlexibleWidth; 17 customScrollView.AddSubview(imageView); 18 19 20 customScrollView.PagingEnabled = false; // freeform scrolling 21 customScrollView.ContentSize = imageView.Frame.Size; 22 customScrollView.MaximumZoomScale = 4f; 23 customScrollView.MinimumZoomScale = 0.25f; 24 customScrollView.Bounces = false; 25 customScrollView.BouncesZoom = false; 26 customScrollView.IndicatorStyle = UIScrollViewIndicatorStyle.White; 27 customScrollView.ViewForZoomingInScrollView = delegate (UIScrollView sender) { 28 return imageView; 29 }; 30 customScrollView.ContentOffset = new System.Drawing.PointF(250, 20); 31 customScrollView.ZoomScale = 1.3f; // 32 var customViewController = new MyViewController(); 33 customViewController.Add(customScrollView); 34 customViewController.Add(text); 35 this.View.Add(customViewController.View);}
7,ok,将要使用的图像文件添入到项目,并保证其右键Build Action属性为Content。程序运行效果图如下: