我的視圖有一個 UIButton,它是通過將角半徑設定為視圖高度的一半而形成的藥丸形狀。一切都很好,但是當它影片(通過旋轉擺動)時,角半徑似乎發生了變化,使藥丸形狀變形。當影片結束時一切恢復正常:
正常外觀:

影片外觀(見圓角半徑):

此處顯示了此記錄:
https://www.dropbox.com/s/xoowpfzdf0ijuql/Simulator Screen Recording - iPhone 12 - 2021-10-08 at 15.05.14.mp4?dl=0
影片代碼 (Xamarin c#) 如下所示:
public static void Wiggle(UIView view, double duration = 0.1f, float repeatCount = 3.0f)
{
UIView.Animate(duration,
() => view.Transform = CGAffineTransform.MakeRotation((nfloat)(-5f * Math.PI / 180f)),
() =>
{
UIView.AnimateNotify(duration, 0.0f, UIViewAnimationOptions.Repeat | UIViewAnimationOptions.Autoreverse | UIViewAnimationOptions.AllowUserInteraction,
() =>
{
UIView.SetAnimationRepeatCount(repeatCount);
view.Transform = CGAffineTransform.MakeRotation((nfloat)(5f * Math.PI / 180f));
},
(animationFinished) =>
{
UIView.Animate(duration, 0, UIViewAnimationOptions.AllowUserInteraction,
() => { view.Transform = CGAffineTransform.MakeIdentity(); }, null);
});
});
}
正如視頻中所見,它并不總是發生。有時藥丸形狀仍然存在。它發生在模擬器和真實設備上。有什么線索嗎?謝謝
uj5u.com熱心網友回復:
你不顯示你的“PillShape”的代碼,但我假設你是“圓角半徑設定為半視圖高度”的LayoutSubviews()?
如果是這樣,問題很可能是當您旋轉按鈕時,它的高度會發生變化,并且您不再具有正確的半徑。
您最好使用圖層影片,而不是旋轉視圖本身。
我不使用 Xamarin/C#,所以這是基于 Objective-C 代碼......希望它能讓你走上正軌。
這使用了一個緩慢(0.5 秒)的影片,可以很容易地看到半徑在旋轉程序中不會改變。點擊按鈕開始/停止影片:
using Foundation;
using System;
using UIKit;
using CoreAnimation;
using ObjCRuntime;
namespace QuickTestApp
{
public partial class ViewController : UIViewController
{
public WigglePillButton btn { get; private set; }
public bool isWiggling { get; private set; }
public ViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
btn = new WigglePillButton();
btn.TranslatesAutoresizingMaskIntoConstraints = false;
btn.SetTitle("Testing", UIControlState.Normal);
btn.BackgroundColor = UIColor.SystemBlueColor;
View.AddSubview(btn);
// Get the parent view's layout
var margins = View.SafeAreaLayoutGuide;
btn.CenterXAnchor.ConstraintEqualTo(margins.CenterXAnchor).Active = true;
btn.CenterYAnchor.ConstraintEqualTo(margins.CenterYAnchor).Active = true;
btn.WidthAnchor.ConstraintEqualTo(120).Active = true;
btn.AddTarget(this, new Selector("ButtonClickAction:"), UIControlEvent.TouchUpInside);
isWiggling = false;
}
[Export("ButtonClickAction:")]
public void ButtonClickAction(UIButton sender)
{
if (isWiggling)
{
btn.StopAnim();
} else {
btn.StartAnim();
}
isWiggling = !isWiggling;
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
// Release any cached data, images, etc that aren't in use.
}
}
public class WigglePillButton : UIButton
{
public override void LayoutSubviews()
{
base.LayoutSubviews();
var maskingShapeLayer = new CAShapeLayer()
{
Path = UIBezierPath.FromRoundedRect(Bounds, 12).CGPath
};
Layer.Mask = maskingShapeLayer;
}
public void StartAnim()
{
AddAnimations();
}
public void StopAnim()
{
Layer.RemoveAllAnimations();
}
private void AddAnimations()
{
CATransaction.Begin();
Layer.AddAnimation(RotAnim(), "rot");
CATransaction.Commit();
}
private CAKeyFrameAnimation RotAnim()
{
CAKeyFrameAnimation animation = CAKeyFrameAnimation.FromKeyPath("transform.rotation.z");
double angle = 5f * Math.PI / 180f;
animation.Values = new[] { NSNumber.FromDouble(angle), NSNumber.FromDouble(-angle) };
animation.AutoReverses = true;
animation.Duration = 0.5;
animation.RepeatCount = float.PositiveInfinity;
return animation;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/324365.html
標籤:ios 动画片 xamarin.ios
