【问题标题】:Can't set CATiledLayer.FadeDuration in MonoTouch无法在 MonoTouch 中设置 CATiledLayer.FadeDuration
【发布时间】:2010-11-30 10:12:20
【问题描述】:

我正在尝试从CATiledLayer 中删除褪色动画。

根据此已关闭的错误报告:https://bugzilla.novell.com/show_bug.cgi?id=648993

,设置 CATiledLayer.FadeDuration 静态属性不起作用也不应该这样做

Objective-C 中建议的解决方案是继承 CATiledLayer: How to change iphone CATiledLayer fadeDuration?。我已经在 MonoTouch 中实现了这一点,但视图中没有绘制任何内容,尽管 DrawInContext 按预期调用。

重现问题的完整代码如下。一旦我删除 [Export("fadeDuration")] 一切正常(但有动画)。

提前感谢您的帮助。

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

【问题讨论】:

  • 你做对了;我不知道为什么它不适合你。是否有理由在您的示例中覆盖 DrawInContext?
  • Miguel,我重写了 DrawInContext 来给你一个设置断点的地方,你自己看看它被调用了。我猜我的 fadeDuration 导出在某种程度上与 MonoTouch 为 CATiledLayer 提供的内容冲突。

标签: ios xamarin.ios catiledlayer


【解决方案1】:

您没有保留对 UIWindow 的引用,这意味着它可以被收集和释放。

我像这样重写了你的课程:

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

并将其放置在基于窗口的项目的默认项目模板中,它按预期工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-16
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    相关资源
    最近更新 更多