How directly initialize View in PanGestureRecognizer - ios

In my ViewController I have three views.
I want to add UIPanGestureRecognizer in this.
Below is my code...
var circlePanGesture = UIPanGestureRecognizer()
var sqaurePanGesture = UIPanGestureRecognizer()
var trianglePanGesture = UIPanGestureRecognizer()
var destination = [UIView]()
#IBOutlet weak var circle_source: UIView!
#IBOutlet weak var square_source: UIView!
#IBOutlet weak var triangle_source: UIView!
override func viewDidLoad() {
super.viewDidLoad()
panGestureConfiguration()
}
func UIConfiguration() {
circle_source.layer.cornerRadius = circle_source.frame.size.width/2
circle_source.clipsToBounds = true
}
func panGestureConfiguration() {
circlePanGesture.addTarget(self, action: #selector(draggedSourceView(_:)))
circle_source.addGestureRecognizer(circlePanGesture)
sqaurePanGesture.addTarget(self, action: #selector(draggedSourceView(_:)))
square_source.addGestureRecognizer(sqaurePanGesture)
trianglePanGesture.addTarget(self, action: #selector(draggedSourceView(_:)))
triangle_source.addGestureRecognizer(trianglePanGesture)
}
In this ViewController's panGestureConfiguration function I want to use only one UIPanGestureRecognizer object instead of three. So how to do it?
Please give me any solution for this.

Related

How add gesture into the tableviewcell?

everyone!
I am trying to add gesture to a UIImageView into the TableViewCell.
When click on the ImageView - the color changes.
class StocksCellView: UITableViewCell {
#IBOutlet weak var logoImageView: UIImageView!
#IBOutlet weak var tickerLabel: UILabel!
#IBOutlet weak var favouriteImageView: UIImageView!{ didSet {
let panGesture = UITapGestureRecognizer(target: self,
action: #selector(gestureAction))
favouriteImageView.addGestureRecognizer(panGesture)
}
}
#IBOutlet weak var companyNameLabel: UILabel!
#IBOutlet weak var priceLabel: UILabel!
#IBOutlet weak var deltaLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
#objc func gestureAction() {
favouriteImageView.tintColor = UIColor.yellow
}
}
But nothing works.
If i change favouriteImageView.addGestureRecognizer(panGesture)
on addGestureRecognizer(panGesture)
Then, by clicking on the cell, the color will change.
But I want by clicking on the picture.
enter image description here
First of all you must check and change like below:
favouriteImageView.isUserInteractionEnabled = true

Not woking UIView ishidden after change UILabel text value in swift

I want to change uilabel value and show uiview that has hide like a popup windows.
When I touch a button, that code print "setPopupView 0".
but doesn't show settingView.
and I touch a button again.
print "setPopupView 0" and show settingView.
so When "settingLabelValueUnit text" has changed not show settingView,
but when "settingLabelValueUnit text" has not changed show settingView.
(It means "settingLabelValue text" is same as input value)
I don't know why.
Anyone can help me?
Thank you
here is my swift code.
class ViewController: UIViewController {
#IBOutlet weak var workoutScrollView: UIScrollView!
#IBOutlet weak var settingView: UIView!
#IBOutlet weak var settingLabelValueUnit: UILabel!
#IBOutlet weak var imgSettingBGcal: UIImageView!
#IBOutlet weak var imgSettingBGdis: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.imgSettingBGcal.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(calSettingClick)))
self.imgSettingBGdis.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(disSettingClick)))
}
func calSettingClick(sender: UITapGestureRecognizer) {
setPopupView(0)
}
func disSettingClick(sender: UITapGestureRecognizer) {
setPopupView(1)
}
func showPopup (_ settype:Int) {
switch settype {
case 0:
print("setPopupView 0")
self.settingLabelValueUnit.text = "Set1"
self.workoutScrollView.isHidden = true
self.settingView.isHidden = false
case 1:
print("setPopupView 0")
self.settingLabelValueUnit.text = "Set2"
self.workoutScrollView.isHidden = true
self.settingView.isHidden = false
}
}
}

How can I access data from a container view from the parent view controller in swift?

I have a view controller(SignUpController) and it has 1 image view at the top, 2 buttons at the bottom and 1 view as container view. The Container view(InfoRegisterController) contains scrollview with several text fields. Container view doesn’t contain any buttons. So, now i need to access text fields of Container view in the parent view controller (or can say as : I need to access all data from container view to parent view controller) so that I can register the user by clicking Register button at the bottom in SignUpController. I couldn’t solve it through delegates and NSUserDefaults too. So , Please help me to solve this in swift 4 ?
This is the parent view controller:
import UIKit
import Material
class SignUpController: UIViewController {
#IBOutlet weak var backgroundView: UIView!
#IBOutlet weak var register: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
backgroundView.layer.borderWidth = 1
backgroundView.layer.borderColor = UIColor.blue.cgColor
register.layer.cornerRadius = 5
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated) // No need for semicolon
let tv: InfoRegisterController = self.childViewControllers[0] as!InfoRegisterController
print("Hello")
//tv.viewWillAppear(true)
}
All data must be accessed to this place so that I can post it to server.
#IBAction func Register(_ sender: Any) {
}
} // class end
Container View:
import UIKit
import Material
class InfoRegisterController: UIViewController, UITextFieldDelegate, UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
#IBOutlet weak var fullName: TextField!
#IBOutlet weak var Email: ErrorTextField!
#IBOutlet weak var Password: TextField!
#IBOutlet weak var verifyPassword: TextField!
#IBOutlet weak var address: TextField!
#IBOutlet weak var permanentAddress: TextField!
#IBOutlet weak var currentAddress: TextField!
#IBOutlet weak var DOB: TextField!
#IBOutlet weak var occupation: TextField!
#IBOutlet weak var selectGender: TextField!
#IBOutlet weak var identificationType: TextField!
#IBOutlet weak var identificationID: TextField!
#IBOutlet weak var issuedDate: TextField!
#IBOutlet weak var issuedDistrict: TextField!
#IBOutlet weak var licenseNumber: TextField!
#IBOutlet weak var fathersName: TextField!
#IBOutlet weak var grandfathersName: TextField!
#IBOutlet weak var mobileNumber: TextField!
override func viewDidLoad() {
super.viewDidLoad()
self.fullName.delegate = self
self.Email.delegate = self
self.Password.delegate = self
self.verifyPassword.delegate = self
self.address.delegate = self
self.permanentAddress.delegate = self
self.currentAddress.delegate = self
self.DOB.delegate = self
self.occupation.delegate = self
self.identificationID.delegate = self
self.issuedDate.delegate = self
self.issuedDistrict.delegate = self
self.licenseNumber.delegate = self
self.fathersName.delegate = self
self.grandfathersName.delegate = self
self.mobileNumber.delegate = self
}
}
Here, I need to access all fields fullName,Email, Password,verifyPassword,address, permanentAddress,currentAddress, DOB,occupation and all others to SignUpController so that I can register new user.
To gain access to the container view from your parent, you will have to pass a reference from the container view to the parent and then use it in the parent view controller, there are many ways to do that and here is one of them.
In your viewDidAppear of InfoRegisterController which is the container view controller add the following code, this method will get a reference of InfoRegisterController into the parent to be used.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let signUpControllerParent = self.parent as! SignUpController
signUpControllerParent.saveContrainerViewRefference(vc: self)
}
Now in SignUpController add a local variable for the coming reference to be saved and used later to get the data from the textfields.
var infoRegisterRefferenceVC : InfoRegisterController?
Add this method also in your parent SignUpController
func saveContrainerViewRefference(vc:InfoRegisterController){
self.infoRegisterRefferenceVC = vc
}
Now you can have access to all the textfields and the methods in the container view from the parent for example:
var fullNameTextField = self.infoRegisterRefferenceVC.fullName.text
This should be it :)
There are several ways to accomplish this. You can use "Delegate" and "Protocol" or prepareForSegue method in parent view controller.
var somePropertyYouWantToAccess: NSString?
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "yourChildViewSegueIdentifier" {
let childVc = segue.destinationViewController as ContainerViewController
self.somePropertyYouWantToAccess = childVc.firstName
}
}

UIActivityIndicator shows for a split second

I am trying to show an spinning activity indicator for my app when the user taps on a button before they are presented with the next view - it kind of works, I see it for a split second
#IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
#IBOutlet weak var eventsbtn: UIButton!
#IBOutlet weak var pubsbtn: UIButton!
override func viewWillAppear(animated: Bool) {
myActivityIndicator.stopAnimating()
myActivityIndicator.hidden = true
}
override func viewDidLoad() {
super.viewDidLoad()
eventsbtn.layer.cornerRadius = 8
pubsbtn.layer.cornerRadius = 8
}
#IBAction func getEvents(sender: AnyObject) {
myActivityIndicator.hidden = false
myActivityIndicator.startAnimating()
}
Refactor your code. You don't need to stop the activity indicator in viewWillAppear. You can hide it in viewDidLoad. Here's the code:
#IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
#IBOutlet weak var eventsbtn: UIButton!
#IBOutlet weak var pubsbtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
eventsbtn.layer.cornerRadius = 8
pubsbtn.layer.cornerRadius = 8
myActivityIndicator.hidden = true
}
#IBAction func getEvents(sender: AnyObject) {
myActivityIndicator.hidden = false
myActivityIndicator.startAnimating()
}
Make sure that the IBOutlet for the activity indicator and IBAction for getting events are correctly connected in Interface Builder.
I changed it and it doesn't work, until I go back in the navigation
#IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
#IBOutlet weak var eventsbtn: UIButton!
#IBOutlet weak var pubsbtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
myActivityIndicator.stopAnimating()
myActivityIndicator.hidden = true
eventsbtn.layer.cornerRadius = 8
pubsbtn.layer.cornerRadius = 8
}
#IBAction func getEvents(sender: AnyObject) {
myActivityIndicator.hidden = false
myActivityIndicator.startAnimating()
}
viewDidAppear is probably a more appropriate place to stop the animation and hide the spinner.

Custom view won't add subview

I have a view controller that's inheriting a view from it's preceding view, like a floating, draggable view controller with a video mini player (like the YouTube app). When I show the second vc, I'm actually hiding its own view before adding all of its subviews to a new, custom view so it seems like the first view controller is still present.
This new, custom view is adding my UIButton, but not my two custom UIViews. One of these custom views contains a tableview, which is nowhere to be found. I'm concerned this may be a constraint problem, where I need to add constraints to the new view programmatically or something like that. Any ideas?
onView = first controller's view
class DetailViewController: UIViewController, UIGestureRecognizerDelegate {
var initialFirstViewFrame: CGRect!
var onView: UIView!
var isExpandedMode: Bool!
var player: UIWebView!
var youtubeFrame: CGRect!
var tblFrame: CGRect!
var restrictOffset: CGFloat!
var restrictTrueOffset: CGFloat!
var restictYaxis: CGFloat!
var transparentView: UIView!
#IBOutlet weak var viewTable: UIView!
#IBOutlet weak var tblView: UITableView!
#IBOutlet weak var viewYouTube: UIView!
#IBOutlet weak var btnDown: UIButton!
override func viewDidLoad() {
var dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.8 * Double(NSEC_PER_SEC)))
dispatch_after(dispatchTime, dispatch_get_main_queue(), {
self.calculateFrames()
})
var pan: UIPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: "panAction")
pan.delegate = self
self.viewYouTube.addGestureRecognizer(pan)
isExpandedMode = true
self.btnDown.hidden = true
}
func calculateFrames() {
youtubeFrame = self.viewYouTube.frame
tblFrame = self.viewTable.frame
UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: UIStatusBarAnimation.Slide)
self.viewYouTube.translatesAutoresizingMaskIntoConstraints() == true
self.viewTable.translatesAutoresizingMaskIntoConstraints() == true
self.viewYouTube.frame = youtubeFrame
self.viewTable.frame = tblFrame
self.player.backgroundColor = UIColor.purpleColor()
self.viewYouTube.backgroundColor = UIColor.greenColor()
restrictOffset=self.initialFirstViewFrame.size.width-200;
restrictTrueOffset = self.initialFirstViewFrame.size.height - 180;
restictYaxis=self.initialFirstViewFrame.size.height-self.viewYouTube.frame.size.height
self.view.hidden = true
transparentView = UIView(frame: self.initialFirstViewFrame)
transparentView.backgroundColor = UIColor.blackColor()
transparentView.alpha = 0.9
self.onView.addSubview(transparentView)
/////// self.viewTable and self.viewYouTube don't load. They have AutoLayout constraints in storyboard and I'm trying to add their views to a different UIView/////////////
self.onView.addSubview(self.viewTable)
self.onView.addSubview(self.viewYouTube)
self.onView.addSubview(self.btnDown)
}

Resources