Pocket Note can be changed the size of UIView,for example UIImage, using pinch gesture and I introduce this method this time.
環境[Environment]:Xcode 8、Swift 3
では早速コードを書いてみましょう。
Let's write code immediately.
import UIKit
class ViewController: UIViewController {
let imageView:UIImageView = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//Pinch Gesture
let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(self.pinchAction(sender:)))
self.view.addGestureRecognizer(pinchGesture)
let image:UIImage = UIImage(named:"sample.jpg")!
let scale:CGFloat = image.size.height / image.size.width
imageView.frame = CGRect(x:0,y:0,width:200,height:200*scale)
imageView.image = image
self.view.addSubview(imageView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
internal func pinchAction(sender: UIPinchGestureRecognizer){
let velocity:CGFloat = sender.velocity
var newWidthValue:CGFloat = imageView.frame.width
var newHeightValue:CGFloat = imageView.frame.height
let scale:CGFloat = newHeightValue / newWidthValue
if (velocity > 0){
//拡大(Scale up)
newWidthValue += 10
newHeightValue += 10*scale
}else{
//縮小(Scale Down)
if (newWidthValue > 10 && newHeightValue > 10){
newWidthValue -= 10
newHeightValue-=10*scale
}
}
imageView.frame = CGRect(x:imageView.frame.minX,y:imageView.frame.minY,width:newWidthValue,height:newHeightValue)
}
}
まずピンチジェスチャーを受け取る為にUIPinchGestureRecognizerを定義します。
そして発生したイベント内でUIPinchGestureRecognizerのvelocityプロパティの値を見て、プラスの値であれば指が広がった操作なのでサイズを拡大し、マイナスの値であれば指が狭まった操作なのでサイズを縮小します。
At first,define UIPinchGestureRecognizer for receiving the pinch gesture.
And when operator use pinch gesture, get value of UIPinchGestureRecognizer's velocity property.
When it is plus, scale up because fingers were spread.
When it is minus, scale down because fingers were narrowed.
At first,define UIPinchGestureRecognizer for receiving the pinch gesture.
And when operator use pinch gesture, get value of UIPinchGestureRecognizer's velocity property.
When it is plus, scale up because fingers were spread.
When it is minus, scale down because fingers were narrowed.
にほんブログ村
クリエイティブライフ ブログランキングへ
0 件のコメント:
コメントを投稿