操纵 s 没有什么特别的技巧UIImageView
。试试下面的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Drawing;
using MonoTouch.CoreGraphics;
namespace SingleFileSolution
{
public class ContentView : UIView
{
public ContentView Companion { get; set; }
public UIImage Image {
get
{
return imageView.Image;
}
set
{
imageView.Image = value;
}
}
private UIImageView imageView;
public ContentView(UIColor fillColor, UIImage initialImage)
{
this.BackgroundColor = fillColor;
imageView = new UIImageView(new RectangleF(10, 60, initialImage.Size.Width, initialImage.Size.Height));
imageView.Image = initialImage;
AddSubview(imageView);
UIButton swapButton = UIButton.FromType(UIButtonType.RoundedRect);
swapButton.SetTitle("Swap Image", UIControlState.Normal);
swapButton.TouchUpInside += (sender, e) => {
if(Companion != null)
{
var tmp = Companion.Image;
Companion.Image = this.Image;
this.Image = tmp;
}
};
swapButton.Frame = new RectangleF(10, 10, 100, 44);
AddSubview(swapButton);
}
}
public class SimpleViewController : UIViewController
{
public SimpleViewController() : base ()
{
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
}
public override void ViewDidLoad()
{
var view1 = new ContentView(UIColor.Blue, UIImage.FromFile("image1.jpg"));
var view2 = new ContentView(UIColor.Red, UIImage.FromFile("image2.jpg"));
view1.Frame = new RectangleF(0, 0, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height / 2);
view2.Frame = new RectangleF(0, UIScreen.MainScreen.Bounds.Height / 2, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height / 2);
view1.Companion = view2;
view2.Companion = view1;
var view = new UIView(new RectangleF(new PointF(0, 0), UIScreen.MainScreen.Bounds.Size));
view.AddSubview(view1);
view.AddSubview(view2);
this.View = view;
}
}
[Register ("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
UIWindow window;
SimpleViewController svController;
UINavigationController navController;
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
window = new UIWindow(UIScreen.MainScreen.Bounds);
svController = new SimpleViewController();
window.RootViewController = svController;
window.MakeKeyAndVisible();
return true;
}
}
public class Application
{
static void Main(string[] args)
{
UIApplication.Main(args, null, "AppDelegate");
}
}
}