- (void)viewDidLoad
{
self.imageview.image = [UIImage imageNamed:@"Image.png"];
[self.view addSubview: self.imageview];
[super viewDidLoad];
}
此代码用于移动图像
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
self.imageview.center = touchLocation;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// get touch event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.PlayVideo];
if ([touch view] == self.view)
{
self.imageview.center = touchLocation;
}
}
此代码用于旋转图像(T将旋转手势放到图像上并拖到.h文件中)
- (IBAction)regRotation:(UIGestureRecognizer *)sender
{
NSLog(@"rotation");
CGFloat netRotation =0.0;
CGFloat rotation =[(UIRotationGestureRecognizer *)sender rotation];
CGAffineTransform transform=CGAffineTransformMakeRotation(rotation+netRotation);
sender.view.transform=transform;
if(sender.state==UIGestureRecognizerStateEnded)
{
netRotation+=rotation;
}
}
图像移动而不是完美旋转
答案 0 :(得分:7)
CGFloat firstX;
CGFloat firstY;
CGFloat lastRotation;
- (void)viewDidLoad
{
lastRotation = 0.0;
self.imageview.image = [UIImage imageNamed:@"Image.png"];
[self.view addSubview: self.imageview];
[super viewDidLoad];
UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[self.imageview addGestureRecognizer:panRecognizer];
UIRotationGestureRecognizer* rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[rotationRecognizer setDelegate:self];
[self.view addGestureRecognizer:rotationRecognizer];
}
-(void)move:(id)sender {
[self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[[sender view] setCenter:translatedPoint];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
CGFloat finalX = translatedPoint.x + (0*[(UIPanGestureRecognizer*)sender velocityInView:self.imageview].x);
CGFloat finalY = translatedPoint.y + (0*[(UIPanGestureRecognizer*)sender velocityInView:self.imageview].y);
[[sender view] setCenter:CGPointMake(finalX, finalY)];
}
}
-(void)rotate:(id)sender {
if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastRotation = 0.0;
return;
}
CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);
CGAffineTransform currentTransform = self.imageview.transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
[self.imageview setTransform:newTransform];
lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
}
它为我工作..
答案 1 :(得分:2)
Swift 4回答Pan,Zoom和Rotate:
var lastRotation : CGFloat = 0.0
var previousScale : CGFloat = 1.0
var beginX : CGFloat = 0.0
var beginY : CGFloat = 0.0
override func viewDidLoad(){
super.viewDidLoad()
let rotationGesture = UIRotationGestureRecognizer.init(target: self, action: #selector(ViewController.rotate(sender:)))
self.imageView.addGestureRecognizer(rotationGesture)
let pinchGesture = UIPinchGestureRecognizer.init(target: self, action: #selector(ViewController.scale(sender:)))
self.imageView.addGestureRecognizer(pinchGesture)
let panGesture = UIPanGestureRecognizer.init(target: self, action: #selector(ViewController.pan(sender:)))
panGesture.minimumNumberOfTouches = 1
panGesture.maximumNumberOfTouches = 1
self.imageView.addGestureRecognizer(panGesture)
}
@objc func rotate(sender : Any){
if let sender = sender as? UIRotationGestureRecognizer, let imageView = self.imageView{
if sender.state == .ended{
lastRotation = 0.0
return
}
let rotation : CGFloat = 0.0 - (lastRotation - sender.rotation)
let currentTransform = imageView.transform
let newTransform = currentTransform.rotated(by: rotation)
imageView.transform = newTransform
lastRotation = sender.rotation
}
}
@objc func scale(sender : Any){
if let sender = sender as? UIPinchGestureRecognizer, let imageView = self.imageView{
if sender.state == .ended{
previousScale = 1.0
return
}
let newScale = 1.0 - (previousScale - sender.scale)
let currentTransform = imageView.transform
let newTransform = currentTransform.scaledBy(x: newScale, y: newScale)
imageView.transform = newTransform
previousScale = sender.scale
}
}
@objc func pan(sender : Any){
if let sender = sender as? UIPanGestureRecognizer, let imageView = self.imageView{
var newCenter = sender.translation(in: self.view)
if(sender.state == .began){
beginX = imageView.center.x
beginY = imageView.center.y
}
newCenter = CGPoint.init(x: beginX + newCenter.x, y: beginY + newCenter.y)
imageView.center = newCenter
}
}