How do I trigger an event when a UIImageView reaches a certain location? - objective-c

I have a UIImageView that moves around the screen, and I am trying to trigger a method when it is within a certain distance of another UIImageView, also moving. There is not necessarily any specific time I want to check, I want to know anytime it is within that distance. Help!
EDIT: I have multiple objects(the enemies) moving down the screen while the player is at the bottom of the screen. When the user taps the screen, it "shoots"(a new projectile object is created and the animation to move it up the screen is started). I am trying to detect when the "projectiles" hit the "enemies" and triggering an event(i.e. killing the enemy). I have no idea where to put the code to do this, or even what to do. Do I want to use the Notification System, or do I want to calculate if the projectile will hit the enemy as soon as it is fired?

You probably want to use Key-Value Observing. Here's an example.

Related

App Inventor: Making one button change more than one variable

This is about App Inventor 2, i had a button which could change depending on what image sprite had been activated. However, i deleted this button accidentally and lost all of the blocks assigned to it. I cannot remember how to do it. Could someone please tell me how I create a button that can change the value of more than one variable depending on what image sprite has been activated?
You can set specific counter for specific sprite. Just set the counter when the sprite is touched. Then, when the button is clicked, you need to use if and else function to check for what sprite is touched. Below is an example of code, yet it is not complete:

iOS horizontal swipe room selection

I am making an iOS app for remote controlling the lights in my house. I have a list of groups of bulbs (rooms) that can be turned on and off. My problem is selecting a room, I want it to be like this:
You can either press the arrows left or right to switch the room, or you can put your finger, where it says Living Room and swipe left or right to switch it.
Can someone give me a pointer as how to implement a swipe feature like that? I know there are gesture recognizers. I never used them, but I guess they won't automatically make the text I want to swipe follow my finger.
I guess it would be possible to use the gesture recognizer and manually move the text to get the swipe "feeling". It just seems to me like there should be an easier solution.
I've done something similar several times, and the most elementary way to approach it is to override the touches... methods on NSResponder. This will tell you when a user's finger touches the component, when it moves, and when they let go. From that, it's pretty easy to adjust the position of your UIView, do some custom rendering, or whatever else you need.
Another, higher-level, approach is to use a UIScrollView and implement it's delegate methods. Inside the scroll view are three elements which completely fill the viewport: the one you can see, and those to the left and right. You watch the delegate events for the user adjusting the position of the viewport and as they expose an element to the left or right, you can snap the viewport into place when they let go. Then, you adjust the three inner views to show the appropriate new content and reset the viewport to expose the middle element (which would then be showing the appropriate content).
There are undoubtedly many other approaches, but I've used both of these with good effect.
I solved it in the end by using a pangesturerecgonizer attached to the label. It's pretty straight forward, I just didn't know pan is supposed to mean something like drag and drop, I thought pan would be something completely different.

Checking an SKNode is onscreen and visible

Is there a way to check when a SKSpriteNode / SKNode is onscreen (i.e. visible) I have a large scrolling background where I am spawning mobs, but I want to limit their animations and sounds if they are not visible. Is there a way to do this, I could write something in the update loop but I wanted to see if there was anything I could test before I start querying mob positions every frame?
I think you are doing this wrong. You should stick to the MVC pattern. As such, you only have data/points that are moving in a large area and in update method, if any data is within screen area, only would you draw them. If it is out, remove them.

How to Insert/Remove/Update UITableView without it appearing to move?

After reading this, I'm interested in being able to dynamically update a UITableView without the view scrolling. That is, if the 12th row is visible, and some background process runs and calls for a new row to be inserted at position 0, how can that be done without the table appearing to scroll?
The closest I've been able to come to achieving this is to do any and all updates in a single -beginUpdates/-endUpdates block and to adjust the contentOffset as appropriate after that. But some of my code might have a bunch of calls to -insertRowsAtIndexPaths:withRowAnimation called outside such a block, and when I do that, the thing can scroll all over the place.
How can I make UITableView stand still?

object position relative to a different subview

I am thinking about creating a Game Board view with an array of subviews to form slots for tiles. I plan to have tiles that I can drop onto the board and have them snap into position. The part I am unclear about is when I drop my tile and the touchesEnded event fires, what is the best way to loop through the subviews of my Game Board to see which slot I am over so I can have the tile snap into proper position? Or is there a better way to keep track of all the "slot" positions?
I really don't want to have to hardcode every cell position and then keep track of it if the Game Board ever gets shifted in my view controller.
Check out hitTest:withEvent: and pointInside:withEvent:.
ditto imaginaryboy. you don't need to loop through anything. let UIKit do the heavy work for you. Compute the center point of the piece that was dropped on your board and the use hitTest:withEvent: on the view containing your board.