TouchesBegan with 2 fingers - objective-c

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.

Related

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.

SKShapeNode with SKAction touch end when moves away from touch location

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.

UITableView.ScrollToTop - Change what it considers the "top"

I'd like to be able to specify the offset or more specifically the cell I consider the "top" of my UITableView when the status bar is pressed and the ScrollToTop occurs.
To give this question a bit of context, its an app that plays music and the table view is the playlist. I'd like that when the user taps the status bar, it scrolls back to the current song, rather than continues scrolling past it into the previously played, grayed out songs. Whilst I agree which not changing default behavior, I think that this would be the expected behavior for a user in this scenario, and several people i've asked agree with me. As to whether its possible is a different story.
You cannot modify the position directly, but you can implement the scrollViewShouldScrollToTop: delegate method to manually scroll to the appropriate position and then return NO to prevent the default behaviour.

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.

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.