Is there any way to access a property like SKSpriteNode in Swift? - ios

I have some experience in SKSpriteKit and I am writing code in UIViewController using UIKit library. And I wonder if I could access existing propteries such as ones from UILabel, UITextField, UIView, and so on. In Swift, nodes can be named and called by their names using childWithName(name: String), so is there any way to do this in UIKit. ? I thought doing this would be much more convenienet and my writing would go smooth. I started programming in UIKit so I do not know much about it, so I appreciate if you teach me abou this!
Thankyou

You can't give "names" to UIViews as far as I'm concerned. However, there's something similar.
You can set a view's tag by doing:
someView.tag = 1
Then, assuming that superView is the super view of someView, you can do this to get someView:
superView.viewWithTag(1)
It's not as descriptive as name, but you can use named constants to make it clearer:
let scoreLabelTag = 1
superView.viewWithTag(scoreLabelTag)

Related

Understanding UIView Animation In Swift

I know how to animate UIViews using the animate() function. What I'm trying to understand is why the animation takes place on the class in general, as opposed to a specific instance. I don't know the right words to describe this so I couldn't find any resources on this.
So for example, if I wanted to use SpriteKit and I wanted to move a node, I'd use node.run(SKAction.move(to: CGPoint.zero, 0.5). This makes a lot of sense to me. It defines an SKAction (so it could be used with other nodes if needed), and then applies it to a specific node.
If I wanted to do the same with a view, I'd do something like:
exampleViewConstraint.constant = 0
UIView.animate(withDuration: 0.5) { [weak self] in
self?.view.layoutIfNeeded()
}
Aside from this being much more complicated verbiage, I don't understand why this is being called on the class in general. I'm having a hard time conceptualizing. What exactly is happening here?
When you call a function on a general class, what does that do? Are there other examples where that's used? Why do you need to use a class function here?
I get that the closure captures the parent class's view, but what if there was another view elsewhere in the same function? How could I specify that it animates view 1 but not view 2?
Why must this be done in such a confusing way?
Thanks in advance for your help!

I searched hard but can't find a way to change controllers order

in swift I'd like to programatically re-arranger the order
of my control
by example I have a textview that is on top of another one
and I'd like to change their orders in which they appear (send to back)ect
I looked hard online but can't find an answer to this
update:
I read the article linked but those solutions don't work in swift 4.0
You have following methods to manipulate which subview is above/behind other:
bringSubviewToFront(_:)
sendSubviewToBack(_:)
removeFromSuperview()
insertSubview(_:atIndex:)
insertSubview(_:aboveSubview:)
insertSubview(_:belowSubview:)
exchangeSubviewAtIndex(_:withSubviewAtIndex:)
If you need to know current index(z position) of subview, it corresponds to its index in superview.subviews array.

Array of UITextView's that interact with objects of the other classes

I'm rather new to objective C and at the moment I'm trying to create one small project.
The task I want to accomplish is the following:
I've got the UIViewController for the screen of the game I'm creating. It has an UIImageView and a UITextView on it. What it does so far is that the latter one is moving towards the former one. And when their frames intersect (CGRectIntersectsRect) some actions happen.
What I want to achieve next is to create a specific class for UITextviews, so that there will be many of them created on the screen of UIViewController (I think array should be used here). And next I want every of them to be checking themselves, if they have an intersection with the UIImageView - than (again) something happens.
I've tried several ways like creating a mutable array, but every time I've some errors connected with variables of the original ViewController used inside of the new class (Hit).
The code I use for the one existing UITextView, that is created inside of UIViewController, is the following:
-(void)Moving{
HitR.center = CGPointMake(HitR.center.x+HitRX, HitR.center.y+HitRY);
if (CGRectIntersectsRect(HitR.frame, Finish.frame)) {
/*some actions here*/
}
}
etc
Can you help me to create these array of UItextFields, using their own class, tell them what to do with the help of properties like UIimageview.frame from the ViewController and then to place them on the screen.
P.S. I've read numerous articles about how to transfer variables from one class to another, but still failed to accomplish my aim.

xcode 4 drawing rectangles

i'm very new to xcode. i've googled this and even serched here and i didn't find exactly what i was looking for. i really wanna get this. especially since it's the most basic thing there is.
so, i have the .h and .m files
i want to DRAG a uiview into the .xib file, i don't want it to be as big as the screen, so i resize it.
now i want to draw a bunch of rectangles INSIDE this view object.
how do i go about doing this?
your help would be greatly appreciated!
So you add a generic UIView then create a subclass of UIView and do your drawing inside the drawRect method.
Then change the class of the UIView to your subclass name in the inspector.
Be sure to #include your header in the app delegate.
That's the basic thing.
For more sophistication, you're going to want to learn to use UIViewController subclasses as well. These are used all over ios.

Select a range in UISlider

i asked me whether it is possible if i can create a uislider who has 2 handles to select a range. Just like here:
The problem i am facing is that i dont want to use a custom UIControl Subclass. I need a UISlider subclass or a other solution for this problem, because a lot of the code is based on UISlider specific propertys etc. So is there any possibility to achieve this ?
Look at the following example:
http://www.cocoacontrols.com/platforms/ios/controls/rangeslider
You can subclass UISlider, but it will be very difficult. Your class should offer quite some new properties, and the old ones won't make much sense at all.
Not sure how your code can be based much on UISlider specific things - as everything would change the meaning (i.e. ranges instead of one value).
If you really need a common base class, you could encapsulate ("has-a" relationship) the control in a custom class and let this handle the different types.
I implemented a similar control using a custom view, and it happened to be quite straight forward.
UISlider doesn't provide the functionality you're after, and subclassing UISlider probably won't work out. What would the value of such a control be? The value of a slider is a number, but you want it to be a range. Consider a custom control that duplicates the UISlider properties you need.

Resources