gpt4 book ai didi

ios - 处理 CAKeyFrameAnimation 的正确方法

转载 作者:行者123 更新时间:2023-11-28 22:10:28 25 4
gpt4 key购买 nike

处理具有自定义事件的 CAKeyFrameAnimation 的正确方法是什么?我制作了一个测试项目,以尽可能简单的方式演示了我正在做的事情。

Here is my Xamarin Studio Project

Here is my XCode Instruments Memory Allocations Trace

要重现我在此内存分配跟踪期间所做的事情,只需运行该项目。按绿色按钮 10 次。 (每次按下绿色按钮时,蓝色方 block 都会动画)。
然后评估内存使用情况。请注意,在我的 CreateAnimationEventHandler_Position_AnimationStopped 函数中,我正在处理动画。 但还要注意,Instruments 告诉我有大量非对象正在泄漏内存...这是什么原因造成的?我该如何正确处理 CAKeyFrameAnimations?

我尝试了几种不同的方法来处理它。这是我的结果。仅按下 10 次按钮后,我获得了大约 30-100kb 的非对象吸收内存。我一定做错了什么。

Without Disposal

With Disposal on animation only not path nor events, more details

With Disposal, more details

这里有一些代码可供查看(但它也在附加的项目中):

public override void ViewDidLoad ()
{
base.ViewDidLoad ();

// Perform any additional setup after loading the view, typically from a nib.



UIImageView ViewToAnimation = new UIImageView(new RectangleF(100, 100, 100, 100));
ViewToAnimation.BackgroundColor = UIColor.Blue;
View.AddSubview(ViewToAnimation);



GoButton = new UIButton(new RectangleF(0, 0, 100, 100));
GoButton.BackgroundColor = UIColor.Green;
View.AddSubview(GoButton);
GoButton.TouchUpInside += delegate {
animation = CreateAnimation_Position(ViewToAnimation, new Random().Next(0,500), new Random().Next(0,700), ViewToAnimation.Center.X, ViewToAnimation.Center.Y, 0.4f, CAMediaTimingFunction.FromName (CAMediaTimingFunction.Linear));
ViewToAnimation.Layer.AddAnimation(animation, "animation");
};


}


public CAKeyFrameAnimation CreateAnimation_Position ( UIView view, float toX, float toY, float fromX, float fromY, float duration_s, CAMediaTimingFunction timingFunction )
{
AppDelegate appDel = (AppDelegate)UIApplication.SharedApplication.Delegate;

CAKeyFrameAnimation positionAnimation = CAKeyFrameAnimation.GetFromKeyPath ("position");
positionAnimation.Duration = duration_s;
positionAnimation.TimingFunction = timingFunction;

PointF toLocation = new PointF(toX, toY);

// Make a path for this animation
CGPath path = new CGPath();
PointF[] lines = {new PointF(fromX, fromY), new PointF(toX, toY)};
path.AddLines(lines);

positionAnimation.Path = path;

event_started = CreateAnimationEventHandler_Position_AnimationStarted(view, toLocation);
positionAnimation.AnimationStarted += event_started;

event_stopped = CreateAnimationEventHandler_Position_AnimationStopped(view, positionAnimation);
positionAnimation.AnimationStopped += event_stopped;

return positionAnimation;
}


public EventHandler CreateAnimationEventHandler_Position_AnimationStarted ( UIView view, PointF toLocation )
{
EventHandler animationStartedEvent = delegate {
// Apply the Position change to the Layer
view.Layer.Position = toLocation;
};

return animationStartedEvent;
}


public EventHandler<CAAnimationStateEventArgs> CreateAnimationEventHandler_Position_AnimationStopped ( UIView view, CAKeyFrameAnimation animationToDispose )
{
EventHandler<CAAnimationStateEventArgs> animationStoppedEvent = delegate(object sender, CAAnimationStateEventArgs e) {
// Console.WriteLine("Finished! finished = " + e.Finished);


// Console.WriteLine("Removing and Disposing Events");
// animationToDispose.AnimationStarted -= event_started;
// animationToDispose.AnimationStopped -= event_stopped;
// event_started = null;
// event_stopped = null;
//
//
// Console.WriteLine("Disposing Animation");
// animationToDispose.Path.Dispose();


// For some reason, only calling Dispose on the animation and NOT calling dispose on the Path & events seems to be more efficient... no idea why
animationToDispose.Dispose();
};

return animationStoppedEvent;
}

最佳答案

Xamarin Support 的优秀人员为我提供了一些关于良好内存实践的好资源,并向我展示了非对象并不总是内存泄漏。在这种情况下,他们不是。所以事实证明我处理正确。

感谢所有阅读本文并考虑提供帮助的人。

http://docs.xamarin.com/guides/cross-platform/application_fundamentals/memory_perf_best_practices/

关于ios - 处理 CAKeyFrameAnimation 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23067079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com