0

I'm trying to remove fading animation from CATiledLayer.

Setting CATiledLayer.FadeDuration static property doesn't work and is not supposed to, according to this closed bug report: https://bugzilla.novell.com/show_bug.cgi?id=648993

The suggested solution in Objective-C is to subclass CATiledLayer: How to change iphone CATiledLayer fadeDuration?. I've implemented this in MonoTouch but nothing is being drawn in the view, although DrawInContext is called as expected.

The complete code to reproduce the problem is below. As soon as I remove [Export("fadeDuration")] everything works (but with animation).

Thanks in advance for any help.

using System;
using System.Drawing;
using MonoTouch.CoreAnimation;
using MonoTouch.UIKit;
using MonoTouch.Foundation;
using MonoTouch.ObjCRuntime;

[Register("NoFadeTiledLayer")]
public class NoFadeTiledLayer : CATiledLayer {

 public NoFadeTiledLayer () : base() {}
 public NoFadeTiledLayer (IntPtr handle) : base(handle) {}

 public override void DrawInContext (MonoTouch.CoreGraphics.CGContext ctx) {
   // This is being called everytime
   base.DrawInContext (ctx); 
 }

 [Export("fadeDuration")]
 public static new double FadeDuration () {
  return 0;
 }
}

public class ContentView : UIView {

 [Export("layerClass")]
 public static Class LayerClass () {
  return new Class (typeof(NoFadeTiledLayer));
 }

 public override void Draw (RectangleF rect) {
  DrawString ("Lorem ipsum", rect, UIFont.SystemFontOfSize (15));
 }
}


[Register("AppDelegate")]
public class AppDelegate : UIApplicationDelegate {

 static void Main (string[] args) {
  UIApplication.Main (args, null, "AppDelegate");
 }

 public override bool FinishedLaunching (UIApplication app, NSDictionary options) {
  var window = new UIWindow (UIScreen.MainScreen.ApplicationFrame);
  window.BackgroundColor = UIColor.Green;
  var view = new ContentView ();
  view.BackgroundColor = UIColor.White;
  view.Frame = window.Bounds;
  window.AddSubview (view);
  window.MakeKeyAndVisible ();

  return true;
 }
}
Community
  • 1
  • 1
Alex Shirshov
  • 635
  • 3
  • 8
  • You are doing things right; I am not sure why it is not drawing for you. Is there a reason for overriding DrawInContext in your sample? – miguel.de.icaza Dec 01 '10 at 07:06
  • Miguel, I overrode DrawInContext to give you a place to set the breakpoint and see for yourself that it's being called. I'm guessing that my export of fadeDuration somehow conflicts with what MonoTouch provided for CATiledLayer. – Alex Shirshov Dec 01 '10 at 07:17

1 Answers1

2

You aren't keeping a reference to your UIWindow, meaning it can be collected and released.

I rewrote your class like so:

using System;
using System.Drawing;
using MonoTouch.CoreAnimation;
using MonoTouch.UIKit;
using MonoTouch.Foundation;
using MonoTouch.ObjCRuntime;

namespace FadeTest
{
    [Register("NoFadeTiledLayer")]
    public class NoFadeTiledLayer : CATiledLayer
    {

        public NoFadeTiledLayer () : base()
        {
        }

        public NoFadeTiledLayer (IntPtr handle) : base(handle)
        {
        }

        public override void DrawInContext (MonoTouch.CoreGraphics.CGContext ctx)
        {
            // This is being called everytime
            base.DrawInContext (ctx); 
        }

        [Export("fadeDuration")]
        public static new double FadeDuration
        {
            get
            {
                return 0;
            }
        }
    }

    public class ContentView : UIView
    {

        [Export("layerClass")]
        public static Class LayerClass ()
        {
            return new Class (typeof(NoFadeTiledLayer));
        }

        public override void Draw (RectangleF rect)
        {
            DrawString ("Lorem ipsum", rect, UIFont.SystemFontOfSize (15));
        }
    }

    public partial class AppDelegate : UIApplicationDelegate
    {
        static void Main (string[] args)
        {
            UIApplication.Main (args, null, "AppDelegate");
        }

        public override bool FinishedLaunching (UIApplication app, NSDictionary options)
        {
            window = new UIWindow (UIScreen.MainScreen.ApplicationFrame);
            window.BackgroundColor = UIColor.Green;
            var view = new ContentView ();
            view.BackgroundColor = UIColor.White;
            view.Frame = window.Bounds;
            window.AddSubview (view);
            window.MakeKeyAndVisible ();

            return true;
        }
    }
}

and placed it in the default project template for a window based project, and it worked as expected.

Geoff Norton
  • 5,056
  • 1
  • 19
  • 17