ios - 按钮的动画图像旋转

标签 ios iphone xamarin xamarin.ios

我正在创建一个 UIButton 并将其添加到 UITabBar 之上,在自定义 UITabBarController 中。

var button = new UIButton(new CGRect(x, y, 50, 50));
button.Layer.BorderWidth = 2;
button.Layer.CornerRadius = button.Frame.Height / 2;
button.SetImage(UIImage.FromBundle("Name"), UIControlState.Normal);

this.View.AddSubview(button);
this.View.LayoutIfNeeded();

当我尝试在 ImageView 上添加旋转变换时,它并没有像我期望的那样围绕中心和 z 轴旋转(并且在我阅读的每个文档中都找到了)。

相反,它的动画效果有些不同。
无论我是在 ImageView 或其层上添加变换,都是一样的。
例如,这是旋转 30 度 (pi/180 * 30) 和 10 度。

enter image description here enter image description here

但是,当我将 SublayerTransform 添加到按钮层时,它会按预期进行转换。该方法的问题在于它不能使用 UIView.AnimateCATransaction.Begin 进行动画处理。

PositionAnchorPoint 和其他一切都设置为默认值。我也尝试使用这些属性,但无济于事。

正如您从图像中看到的那样,它是一个 + 符号,应该动画变成一个 x

非常感谢任何提示。

我定义的 3D 变换是这样的:

CATransform3D.MakeRotation((float)(Math.PI / 180 * 45), 0, 0, 1)

正常的是这样的:

CGAffineTransform.MakeRotation((float)(Math.PI / 180 * 45))

最佳答案

如果出于某种原因你只需要旋转按钮 ImageView ,试试这个:

button.ImageView.Layer.Transform = CATransform3D.MakeRotation((float)(Math.PI / 180 * 45), 0.0001f, 0.0001f, 1);

当 x 和 y 值设置为 0 时,似乎 x 和 y 轴上仍然存在旋转,尽管不应该存在。值为 45 度时,它也会旋转 x 和 y,以便您看到图像边缘打开(因此似乎将 x 和 y 旋转 90 度),即它消失了。为 X 和 y 输入较小的值似乎可以避免此错误。我要看看这是 Xamarin 独有的还是 iOS 的东西。

编辑: 我刚刚在 XCode 的 Obj-C 中对此进行了测试并获得了完全相同的行为,因此这不是 Xamarin 问题而是 iOS 的问题以及它如何处理转换UIImageView 位于 UIButton 中。 在 UIButton 中的 UIImageView 不会出现同样的问题。

关于ios - 按钮的动画图像旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54137555/

相关文章:

ios - 需要一些关于 iOS 项目面向对象设计的建议

iPhone : form like new contact

xamarin - 如何在iPhone x中设置安全区域布局

ios - UITableView - 在编辑模式下重复使用单元格

iphone - 如何正确设置 UIViewController 的 navigationController 标题?

iphone - 应用程序在发布自定义表格单元格时崩溃

android - 如何远程唤醒手机?

xamarin - 如何在适用于 Android 的 Xamarin.Forms 应用程序中使用 D8、R8?

ios - 细胞重用问题 swift

iphone - 在 iOS 上,为什么 shouldAutorotateToInterfaceOrientation 被调用 10、12 或 13 次?