IOS: transform attribute of UIView and implementation of drag-and-drop view, uiviewtransform
1. Use of the transform attribute of UIView
# Import "ViewController. h "@ interface ViewController () @ property (weak, nonatomic) IBOutletUIImageView * imageV; @ end @ implementation ViewController-(void) viewDidLoad {[superviewDidLoad];}-(IBAction) moveUp :( id) sender {// translation [UIView animateWithDuration: 0.5 animations: ^ {// use Make, which is a deformation relative to the original position. // self. imageV. transform = CGAffineTransformMakeTranslation (0,-100); // deformation relative to the previous one. self. imageV. transform = CGAffineTransformTranslate (self. imageV. transform, 0,-100);}];}-(IBAction) moveDown :( id) sender {// translation [UIView animateWithDuration: 0.5 animations: ^ {// use Make, it is a deformation relative to the original location. // self. imageV. transform = CGAffineTransformMakeTranslation (0,-100); // deformation relative to the previous one. self. imageV. transform = CGAffineTransformTranslate (self. imageV. transform, 0,100) ;}] ;}- (IBAction) rotation :( id) sender {[UIViewanimateWithDuration: 0.5 animations: ^ {// rotation (rotation degree, which is a radian) // self. imageV. transform = CGAffineTransformMakeRotation (M_PI_4); self. imageV. transform = CGAffineTransformRotate (self. imageV. transform, M_PI_4);}];}-(IBAction) scale :( id) sender {[UIView animateWithDuration: 0.5 animations: ^ {// zoom // self. imageV. transform = CGAffineTransformMakeScale (0.5, 0.5); self. imageV. transform = CGAffineTransformScale (self. imageV. transform, 0.8, 0.8) ;}] ;}@ end
2. simple use of the touch method of UIView to achieve the effect of dragging a view
# Import "RedView. h" @ implementation RedView // call-(void) touchesBegan :( NSSet
*) Touches withEvent :( UIEvent *) event {NSLog (@ "% s" ,__ func _);} // call when the touch starts to move (it will be called continuously during movement) // NSSet: unordered // NSArray: ordered-(void) touchesMoved :( NSSet
*) Touches withEvent :( UIEvent *) event {// NSLog (@ "% s" ,__ func _); // drag and drop UITouch * touch = [touches anyObject] In UIView; // calculate the offset = X CGPoint curP = [touch locationInView: self]; CGPoint preP = [touchpreviuslocationinview: self]; NSLog (@ "curP = % @", NSStringFromCGPoint (curP); NSLog (@ "preP = % @", NSStringFromCGPoint (preP )); CGFloat offsetX = curP. x-preP. x; CGFloat offsetY = curP. y-preP. y; // translate self. transform = CGAffineTransformTranslate (self. transform, offsetX, offsetY);} // call-(void) touchesEnded :( NSSet
*) Touches withEvent :( UIEvent *) event {NSLog (@ "% s" ,__ func __);} // This method will be called when a system event occurs (call in, automatic shutdown)-(void) touchesCancelled :( NSSet
*) Touches withEvent :( UIEvent *) event {NSLog (@ "% s" ,__ func _);} @ end