快速,旋转图像视图并将旋转的图像视图设置为 uibutton
Posted
技术标签:
【中文标题】快速,旋转图像视图并将旋转的图像视图设置为 uibutton【英文标题】:swift, rotate image view and set rotated image view to uibutton 【发布时间】:2016-01-15 11:49:11 【问题描述】:我想点击按钮并用动画旋转它的图像。动画完成后,我想为按钮设置新的旋转图像。但是由于某种原因,旋转的图像未设置为正常状态的按钮。它设置为突出显示状态。我有布尔值 isPriceOrdered ,根据这个布尔值我设置箭头向上或向下。这是一个代码:
UIView.animateWithDuration(0.3, animations:
self.buttonimage.imageView!.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
, completion:
(success) in
if self.isPriceOrderAsc == true
self.isPriceOrderAsc = false
let rotatedimage = UIImage(CGImage: (self.buttonimage.imageView!.image!.CGImage)!, scale: CGFloat(1.0), orientation: UIImageOrientation.DownMirrored)
self.buttonimage.setImage(rotatedimage, forState: .Normal)
else
self.isPriceOrderAsc = true
let rotatedimage = UIImage(CGImage: (self.buttonimage.imageView?.image!.CGImage)!, scale: CGFloat(1.0), orientation: UIImageOrientation.UpMirrored)
self.buttonimage.setImage(rotatedimage, forState: .Normal)
)
【问题讨论】:
【参考方案1】:问题是我在转换图像之前没有保存按钮的原始转换。在变换图像度数之前保存变换后的原始图像。这是一个代码:
var originalTransform: CGAffineTransform?
UIView.animateWithDuration(0.3, animations:
if self.buttonPriceOrder.imageView != nil
originalTransform = self.buttonPriceOrder.imageView!.transform
self.buttonPriceOrder.imageView!.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
, completion:
(success) in
if success
self.buttonPriceOrder.imageView?.transform = originalTransform!
if self.isPriceOrderAsc == true
self.isPriceOrderAsc = false
let rotatedimage = UIImage(CGImage: (self.buttonPriceOrder.imageView!.image!.CGImage)!, scale: CGFloat(1.0), orientation: UIImageOrientation.DownMirrored)
self.buttonPriceOrder.setImage(rotatedimage, forState: .Normal)
else
self.isPriceOrderAsc = true
let rotatedimage = UIImage(CGImage: (self.buttonPriceOrder.imageView?.image!.CGImage)!, scale: CGFloat(1.0), orientation: UIImageOrientation.UpMirrored)
self.buttonPriceOrder.imageView?.image = nil
self.buttonPriceOrder.setImage(rotatedimage, forState: .Normal)
self.isFilterActive = true
self.delegate.filterUpdated(self)
)
在将图像设置为按钮之前旋转图像后,将按钮变换设置为原始图像。
【讨论】:
以上是关于快速,旋转图像视图并将旋转的图像视图设置为 uibutton的主要内容,如果未能解决你的问题,请参考以下文章
为啥使用相机意图捕获的图像会在 Android 上的某些设备上旋转?