Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class ViewController: UIViewController {
- @IBOutlet weak var mainImageView: UIImageView! //Background Image
- var Person: UIImageView! //Foreground Image
- var location = CGPoint(x: 0, y: 0)
- var image: UIImage!
- var frontSize: CGSize!
- @IBOutlet weak var overlayButton: UIButton!
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view, typically from a nib.
- let image: UIImage = UIImage(named: "pizza")!
- frontSize = image.size
- Person = UIImageView(frame: CGRectMake(90, 140, image.size.width, image.size.height))
- Person.image = image
- Person.contentMode = .ScaleAspectFit
- Person.userInteractionEnabled = true
- self.mainImageView.addSubview(Person)
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- override func viewWillAppear(animated: Bool) {
- super.viewWillAppear(animated)
- }
- override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
- if (segue.identifier == "detailImage") {
- let imageVC = (segue.destinationViewController as! ImageViewController)
- imageVC.image = image
- }
- }
- override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent!) {
- let touch : UITouch! = event.allTouches()?.first
- location = touch.locationInView(self.mainImageView)
- Person.center = location
- }
- override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent!) {
- let touch : UITouch! = event.allTouches()?.first
- location = touch.locationInView(self.mainImageView)
- Person.center = location
- }
- override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
- let size = CGSizeMake(mainImageView.image!.size.width, mainImageView.image!.size.height)
- UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
- mainImageView.image?.drawInRect(CGRectMake(mainImageView.frame.origin.x, mainImageView.frame.origin.y, size.width, size.height))
- Person.image?.drawInRect(CGRectMake(location.x, location.y, frontSize.width, frontSize.height))
- let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
- UIGraphicsEndImageContext()
- //set finalImage to IBOulet UIImageView
- image = newImage
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement