Meme app, draft 1

1 2 3 4 5 |
func createRotateGestureRecognizer(targetView:UIImageView) { var rotateGesture = UIRotationGestureRecognizer(target: self, action: ("handleRotate:")) targetView.addGestureRecognizer(rotateGesture) rotateGesture.delegate = self } |
1 |
("handleRotate:") |
1 2 3 4 5 6 7 8 9 10 11 |
func handleRotate(recognizer : UIRotationGestureRecognizer) { recognizer.view!.transform = CGAffineTransformRotate(recognizer.view!.transform, recognizer.rotation) recognizer.rotation = 0 var label = recognizer.view as UIImageView // creating a new UIImageView out of the rotation if recognizer.state == UIGestureRecognizerState.Changed { hoverShadow(label) // add some Extra Shadow when we are rotating the new ImageView } else { shadow(label) // return to the original Shadow when not moving } } |
1 2 3 4 5 6 |
// The Pan Gesture func createPanGestureRecognizer(targetView: UIImageView) { var panGesture = UIPanGestureRecognizer(target: self, action: ("handlePanGesture:")) targetView.addGestureRecognizer(panGesture) } |
1 |
("handlePanGesture:")) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
// THE HANDLE func handlePanGesture(panGesture: UIPanGestureRecognizer) { // get translation var translation = panGesture.translationInView(view) panGesture.setTranslation(CGPointZero, inView: view) println(translation) //create a new Label and give it the parameters of the old one var label = panGesture.view as UIImageView label.center = CGPoint(x: label.center.x+translation.x, y: label.center.y+translation.y) label.multipleTouchEnabled = true label.userInteractionEnabled = true if panGesture.state == UIGestureRecognizerState.Began { //add something you want to happen when the Label Panning has started } if panGesture.state == UIGestureRecognizerState.Ended { //add something you want to happen when the Label Panning has ended } if panGesture.state == UIGestureRecognizerState.Changed { //add something you want to happen when the Label Panning has been change ( during the moving/panning ) } else { // or something when its not moving } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { var x = CGFloat(arc4random_uniform(100)+50) var y = CGFloat(arc4random_uniform(100)+50) var image = info[UIImagePickerControllerOriginalImage] as UIImage // create a variable from the image var imageViewNew = UIImageView(frame: CGRectMake(100, 100, 150 , 150)) // creating a new UIImageView for the image once loaded on the screen imageViewNew.center = CGPoint(x: x, y: y) imageViewNew.image = image imageView.addSubview(imageViewNew) // add the newly created UIImageView to the main Imageview Subview imageViewNew.multipleTouchEnabled = true imageViewNew.userInteractionEnabled = true // UIImageView by default has these disabled createPanGestureRecognizer(imageViewNew) createPinchGestureRecognizer(imageViewNew) // adding gesture to the newly created image ? Keep in mind you have to create these functions separately. createRotateGestureRecognizer(imageViewNew) dismissViewControllerAnimated(true, completion: nil) } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
@IBAction func imageButtonPressed(sender: AnyObject) { var imagePicker = UIImagePickerController() var sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum if UIImagePickerController.isSourceTypeAvailable(sourceType) { imagePicker.sourceType = sourceType imagePicker.delegate = self // show a screen presentViewController(imagePicker, animated: true, completion: nil) } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
class ViewController: UIViewController { // first creating 2 UILabels var meme1: UILabel! var meme2: UILabel! override func viewDidLoad() { super.viewDidLoad() func flyMemes () { // Creating our function meme1.text = "Brace yourself!" meme1.font = UIFont.systemFontOfSize(25) meme1.textColor = UIColor.whiteColor() // This is all just styling the Label meme1.sizeToFit() meme1.center = CGPoint(x: 200, y: -50) // Starting position of the label view.addSubview(meme1) // Important! Adding the Label to the Subview, so we can actually see it. //Animation options. Play around with it. UIView.animateWithDuration(0.9, delay: 0.0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.0, options: .CurveLinear, animations: { self.meme1.center = CGPoint(x: 200, y:50 ) // Ending position of the Label }, completion: nil) UIView.animateWithDuration(0.6, delay: 1.1, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.0, options: .CurveLinear, animations: { self.meme1.center = CGPoint(x: 200, y:150+90 ) }, completion: nil) |
1 2 |
//call it flyMemes() |