-
Notifications
You must be signed in to change notification settings - Fork 4
stan's version 1 approach #1
base: master
Are you sure you want to change the base?
Changes from all commits
ba8c94b
c5f4c4c
cca7b54
8ff2d9c
02e0f90
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // | ||
| // Drag.swift | ||
| // DragAndDrop | ||
| // | ||
| // Created by Stanley Chiang on 3/6/16. | ||
| // Copyright © 2016 SantaClaraiOSConnect. All rights reserved. | ||
| // | ||
|
|
||
| import UIKit | ||
|
|
||
| protocol draggableViewDelegate { | ||
| func didDragToTrash() -> Bool | ||
| } | ||
|
|
||
| class DraggableView: UIView { | ||
|
|
||
| var panGestureRecognizer:UIPanGestureRecognizer! | ||
| var initialPosition:CGPoint! | ||
|
|
||
| var delegate:draggableViewDelegate! | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delegate should be |
||
|
|
||
| override init(frame: CGRect) { | ||
| super.init(frame: frame) | ||
| initialPosition = frame.origin | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps move this saving of the This is a win for me on 2 points:
|
||
| panGestureRecognizer = UIPanGestureRecognizer() | ||
| panGestureRecognizer.addTarget(self, action: "updatePosition:") | ||
| addGestureRecognizer(panGestureRecognizer) | ||
| } | ||
|
|
||
| required init?(coder aDecoder: NSCoder) { | ||
| fatalError("init(coder:) has not been implemented") | ||
| } | ||
|
|
||
| func updatePosition(sender: UIPanGestureRecognizer) { | ||
| let translation = sender.translationInView(self) | ||
|
|
||
| frame.origin.x = initialPosition.x + translation.x | ||
| frame.origin.y = initialPosition.y + translation.y | ||
|
|
||
| let willTrash: Bool = delegate.didDragToTrash() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: don't force the type to be |
||
|
|
||
| if willTrash { | ||
| layer.borderColor = UIColor.redColor().CGColor | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any way to make the |
||
| layer.borderWidth = 3 | ||
| } else { | ||
| layer.borderWidth = 0 | ||
| } | ||
|
|
||
| if panGestureRecognizer.state == UIGestureRecognizerState.Ended { | ||
| if !willTrash { | ||
| snapToOriginAnimation() | ||
| } else { | ||
| trashActionAnimation() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func snapToOriginAnimation() { | ||
| UIView.animateWithDuration(0.2) { () -> Void in | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Drop the |
||
| self.frame.origin = self.initialPosition | ||
| } | ||
| } | ||
|
|
||
| func trashActionAnimation() { | ||
| UIView.animateWithDuration(0.1, animations: { () -> Void in | ||
| self.alpha = 0 | ||
| }) { (complete) -> Void in | ||
| if complete { | ||
| self.removeFromSuperview() | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // | ||
| // View.swift | ||
| // DragAndDrop | ||
| // | ||
| // Created by Stanley Chiang on 3/6/16. | ||
| // Copyright © 2016 SantaClaraiOSConnect. All rights reserved. | ||
| // | ||
|
|
||
| import UIKit | ||
|
|
||
| protocol ViewDelegate { | ||
| func reset() | ||
| } | ||
|
|
||
| class View: UIView, draggableViewDelegate { | ||
|
|
||
| var draggableView:DraggableView! | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More ImplicitlyUnwrappedOptionals 😟 Also let all the things that you can. |
||
| var trashArea:UIView! | ||
| var resetButton:UIButton! | ||
|
|
||
| var delegate:ViewDelegate! | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| override init(frame: CGRect) { | ||
| super.init(frame: frame) | ||
|
|
||
| trashArea = initTrashArea() | ||
| addSubview(trashArea) | ||
|
|
||
| resetButton = initResetButton() | ||
| addSubview(resetButton) | ||
|
|
||
| draggableView = initDraggableView() | ||
| addSubview(draggableView) | ||
|
|
||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is on the initializer. We don't need it. Since we are already init-ing our views above. override layoutSubviews() {
addSubView(trashArea)
...
}Once we have done that we don't need this initializer any more, |
||
|
|
||
| required init?(coder aDecoder: NSCoder) { | ||
| fatalError("init(coder:) has not been implemented") | ||
| } | ||
|
|
||
| func initDraggableView() -> DraggableView { | ||
| let view = DraggableView(frame: CGRectMake(100,100,100,100)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would love to see this using Auto Layout. 😄 I tried this and what I ended up doing was init with frame zero, then have another function also called in |
||
| view.delegate = self | ||
| view.backgroundColor = UIColor.orangeColor() | ||
| return view | ||
| } | ||
|
|
||
| func initTrashArea() -> UIView { | ||
| let view = UIView(frame: CGRectMake(0, frame.height * 2 / 3,frame.width,frame.height / 3 - 50)) | ||
| view.backgroundColor = UIColor.lightGrayColor() | ||
| return view | ||
| } | ||
|
|
||
| func initResetButton() -> UIButton { | ||
| let button = UIButton(frame: CGRectMake(0,frame.height - 50,frame.width, 50)) | ||
| button.setTitle("Reset", forState: UIControlState.Normal) | ||
| button.setTitleColor(UIColor.lightTextColor(), forState: UIControlState.Normal) | ||
| button.backgroundColor = UIColor.darkGrayColor() | ||
|
|
||
| // TODO: learn more about what it means to pass target as self all the time | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could call However this will not work since you delegate is not necessarily an NSObject subclass, and might not know how to to magical target action stuff. |
||
| button.addTarget(self, action: "resetAction:", forControlEvents: UIControlEvents.TouchUpInside) | ||
| return button | ||
| } | ||
|
|
||
| func resetAction(sender: UIButton){ | ||
| delegate.reset() | ||
| } | ||
|
|
||
| func didDragToTrash() -> Bool { | ||
| return draggableView.frame.intersects(trashArea.frame) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,18 +8,30 @@ | |
|
|
||
| import UIKit | ||
|
|
||
| class ViewController: UIViewController { | ||
| class ViewController: UIViewController, ViewDelegate { | ||
|
|
||
| var mainView:View! | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More 😟 This one is a bit trickier to get rid of... My suggestion is to use lazy evaluation. We can put the lazy keyword in front of this and then instantiate it with a closure. The closure is then run the first time the value is accessed. lazy var mainView: View = {
let v = View(frame: self.view.frame)
v.delegate = self
self.view.addSubView(v)
return v
}()
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My other note is I wish we did not need this main view, perhaps instead of creating this the adding it as a subview we should be overriding |
||
|
|
||
| override func viewDidLoad() { | ||
| super.viewDidLoad() | ||
| // Do any additional setup after loading the view, typically from a nib. | ||
| self.view.backgroundColor = UIColor.whiteColor() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the default? |
||
| mainView = View(frame: self.view.frame) | ||
| mainView.delegate = self | ||
| self.view.addSubview(mainView) | ||
| } | ||
|
|
||
| override func didReceiveMemoryWarning() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove Unused Function. |
||
| super.didReceiveMemoryWarning() | ||
| // Dispose of any resources that can be recreated. | ||
| } | ||
|
|
||
| func reset() { | ||
| mainView.removeFromSuperview() | ||
|
|
||
| mainView = View(frame: self.view.frame) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe combine these repeated lines in some way.
|
||
| mainView.delegate = self | ||
| self.view.addSubview(mainView) | ||
| } | ||
|
|
||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would love to see this not be an ImplicitlyUnwrappedOptional 😄
For this one specifically we can just initialize it directly.
let panGestureRecognizer = UIPanGestureRecognizer()Then remove the declaration from
init()