我想「裁剪」imageView中的圖像。裁剪的意義在於:如果用戶向左平移,則圖片會從右側裁剪到左側。所以,當用戶向右移動時,如果用戶向左端移動,則圖片將完全顯示,圖片不可見。使用PanGestureRecognizer在UIImageView中裁剪UIImage的右側
我試過如下:
@IBAction func handlePanGesture(recognizer: UIPanGestureRecognizer) {
let translation = recognizer.translationInView(containerView)
// Resize the image View
let image = imageView.image!
let hasAlpha = false
let scale: CGFloat = 0.0
let newRect = CGRectMake(0, 0, image.size.width - translation.x, image.size.height)
UIGraphicsBeginImageContextWithOptions(newRect.size, !hasAlpha, scale)
image.drawAtPoint(CGPointMake(-newRect.origin.x, -newRect.origin.y))
let croppedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
imageView.image = croppedImage
recognizer.setTranslation(CGPointZero, inView: containerView)
}
這類作品。查看gif here(目前這隻適用於左圖)。
有人能指出我正確的方向嗎?爲什麼圖像高度不一致,是因爲contentMode設置爲「Aspect Fit」?爲什麼只有在平移到左側時纔會調整大小? Here is a screenshot的配置imageView
感謝您的任何提示。
無論你想甚至可以改變圖像視圖的高度與平移手勢一起??? –
只要寬度應該改變。高度可以保持不變。 – heikomania
即將生病發送給我你正在處理的代碼 –