SKShapeNode with SKAction touch end when moves away from touch location - objective-c

I'm currently making a very simple game to test out the Swift language from Apple, so far so good but I'm stuck with a touchesBegan and touchesEnded events while the SKShapeNode is animating away from the touch location.
The flow is this
Circles are animating around randomly
I touch one of them and it sets it to active
if the circle moves away from that touch it should become inactive
touchesEnded should fire as usual but also when the node moves away/out of the touch location
And I can't think of a way to do this, any help?

First of all, you have to include the code instead of just describing it.
An SKNode (or it's subclasses) do not lose a touch delegated to them even when it moves out of their apparent range. You will actually have to calculate the distance of the touch from the node's centre yourself in the -touchesMoved method and cancel the touch if it passes beyond a range you specify.

Related

TouchesBegan with 2 fingers

I want to track user touches on screen. the user can touch with two fingers and then remove 1 Or touch with one finger and immediately with another (there will be two fingers currently on the screen) then remove one finger. How to exactly track the touch 1 and touch 2
I don't quite understand what you mean by saying "remove 1 Or touch with one finger and immediately with another." I assume what you're looking for is to track multiple touches. If so, see my answer here.
The basic concept for this is to store each UITouch ID in an array when touchesBegan:: called, and later compare each ID with the touches on screen on touchesMoved:: event. This way, each finger can be paired with a single object, and be tracked along when panning.
If you only want to track two fingers, simply set MAX_TOUCHES to 2. Hope this helped.

How can I set the end dragging velocity of a UICollectionView with custom flow layout?

I'm working on a UICollectionView with a custom flow layout subclass which, among other things, does some custom "paging". Everything's fine but for the fact that depending on how I drag, when I release and after - (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity gets called, the collection view (or some part of the UICollectionViewFlowLayout which I did not yet know I need to override) is controlling the velocity with which the animation of an item snapping happens.
That is, if I slightly offset an item from the center of the
collection view qnd release, it snaps back to its position pretty
quickly (desired).
But If I drag the item, say, half way past the collection view's
frame and/or change swiping directions while still dragging and then
release, the "snap" animation takes too long (not desired: I'd like the velocity to adjust so that the end drag animation takes the same amount of time always, regardless of distance).
I tried modifying the decelerationRate of the collection view but it doesn't seem to do anything. And I'm thinking of writing my own animation block in one of the collection view delegate methods, but I'm wondering if there is a different way (perhaps from within the flow layout subclass?).
Well, actually setting self.collectionView.decelerationRate = 0.; seems to work for now. It at least does not decelerate the scrolling and so it looks like constant velocity which is not exactly what I wanted but feels almost right.

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

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.

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.

iPhone UIScrollView, Slow down the scrolling

How do I add some extra drag to the UIScrollView physics. It scrolls just a little too fast for what I am doing. (I don't want to disable altogether, I still like the rubber band effect when you get to the end or beginning of the view.) Is there any way to slow it down?
Actually, after reading the Documentation I discovered the property for UIScrollView that slows down the scrolling, so Apple does make this readily available for anyone else looking for this:
scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
Even though it says fast, it is speeding up deceleration which in effect slows the scrolling down. This was exactly what I needed. And no worry of patents. :)
Actually you can make block animation with ScrollToRowAtIndexPath:... with animated set to NO. This way block animation will animate underlaying variables manually using selected time.
As UICollectionView is subclass of UIScrollView You can use scroll view delegate method.
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
targetContentOffset->x = (targetContentOffset->x - scrollView.contentOffset.x) / 6 + scrollView.contentOffset.x;
targetContentOffset->y = (targetContentOffset->y - scrollView.contentOffset.y) / 6 + scrollView.contentOffset.y;
}
This is simply not possible right now. Maybe Apple will one day extend the API to allow it, but until that day, you're stuck with the default speed.
The only other option would be your own scrolling implementation. This isn't trivial if you want all the nice polish such as the rubber band effect and the way the scrolling will lock horizontally or vertically. Not to mention flicking to scroll.
Also, Apple has a patent for some of those scrolling behaviours, so writing your own might even be dangerous. Source: "Apple's touch-screen patent".
A computing device, comprising: a touch screen display; one or more processors; memory; and one or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, the one or more programs including: instructions for detecting one or more finger contacts with the touch screen display; instructions for applying one or more heuristics to the one or more finger contacts to determine a command for the device; and instructions for processing the command; wherein the one or more heuristics comprise: a vertical screen scrolling heuristic for determining that the one or more finger contacts correspond to a one-dimensional vertical screen scrolling command rather than a two-dimensional screen translation command based on an angle of initial movement of a finger contact with respect to the touch screen display; a two-dimensional screen translation heuristic for determining that the one or more finger contacts correspond to the two-dimensional screen translation command rather than the one-dimensional vertical screen scrolling command based on the angle of initial movement of the finger contact with respect to the touch screen display; and a next item heuristic for determining that the one or more finger contacts correspond to a command to transition from displaying a respective item in a set of items to displaying a next item in the set of items.
But of course, I am not a lawyer.