Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. import UIKit
  2.  
  3. class ViewController: UIViewController {
  4.  
  5. @IBOutlet weak var mainImageView: UIImageView! //Background Image
  6. var Person: UIImageView! //Foreground Image
  7. var location = CGPoint(x: 0, y: 0)
  8. var image: UIImage!
  9. var frontSize: CGSize!
  10.  
  11. @IBOutlet weak var overlayButton: UIButton!
  12.  
  13. override func viewDidLoad() {
  14. super.viewDidLoad()
  15. // Do any additional setup after loading the view, typically from a nib.
  16.  
  17. let image: UIImage = UIImage(named: "pizza")!
  18. frontSize = image.size
  19. Person = UIImageView(frame: CGRectMake(90, 140, image.size.width, image.size.height))
  20. Person.image = image
  21. Person.contentMode = .ScaleAspectFit
  22.  
  23. Person.userInteractionEnabled = true
  24.  
  25. self.mainImageView.addSubview(Person)
  26.  
  27. }
  28.  
  29. override func didReceiveMemoryWarning() {
  30. super.didReceiveMemoryWarning()
  31. // Dispose of any resources that can be recreated.
  32. }
  33.  
  34. override func viewWillAppear(animated: Bool) {
  35. super.viewWillAppear(animated)
  36.  
  37.  
  38. }
  39.  
  40. override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  41. if (segue.identifier == "detailImage") {
  42. let imageVC = (segue.destinationViewController as! ImageViewController)
  43.  
  44. imageVC.image = image
  45. }
  46. }
  47.  
  48. override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent!) {
  49.  
  50. let touch : UITouch! = event.allTouches()?.first
  51.  
  52. location = touch.locationInView(self.mainImageView)
  53.  
  54. Person.center = location
  55.  
  56. }
  57.  
  58. override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent!) {
  59.  
  60. let touch : UITouch! = event.allTouches()?.first
  61.  
  62. location = touch.locationInView(self.mainImageView)
  63.  
  64. Person.center = location
  65.  
  66. }
  67.  
  68. override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
  69. let size = CGSizeMake(mainImageView.image!.size.width, mainImageView.image!.size.height)
  70. UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
  71.  
  72. mainImageView.image?.drawInRect(CGRectMake(mainImageView.frame.origin.x, mainImageView.frame.origin.y, size.width, size.height))
  73. Person.image?.drawInRect(CGRectMake(location.x, location.y, frontSize.width, frontSize.height))
  74.  
  75. let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
  76. UIGraphicsEndImageContext()
  77.  
  78. //set finalImage to IBOulet UIImageView
  79. image = newImage
  80.  
  81. }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement