UITextView get and lost focus events - iphone

How can I get event when my UITextView get and lost focus? I have not found such as events. (UITextView is not editable)

Use override touchesBegan in your code
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event
{
UITouch *touch = [[event allTouches] anyObject];
if ([touch view]==textviewtofocus)
{
// textviewtofocus get focus
}
else
// textviewtofocus lost focus
}

Related

Continuous Touch detection in imageView using Touch event..?

I have a method which call the another view. i want to call this method after the image is continuously for a definite time.
This type of method found in UIButton = touchDown(),touchup().
Is there any method in touch event to find continuous touch in image view.
Pls help me.
thanks in advance
You can use these events for UIIMAGEVIEW:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
You can use the tap gesture which you can see the answer i wrote in this thread.In this you can set the number of taps which needs to fire the event.Hope this helps you.
assuming you want to count the touch (continous like double tap)
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
int index =[touch view].tag;
if (touch.tapCount == number && index == imageTag) {
}
}
tapCount will be the count of tap in continues time with a very short time interval (double tap). you cannot use it if the count of tap you want to watch has longer delay (say 5 single taps). alternatively, you can remember the touches for your imageview, something like:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
int index =[touch view].tag;
if(index == imagetag){
if([tempMutableArray count] < definiteTime){
[tempMutableArray addObject:#"any"]
}else{
[tempMutablArray removeAllObjects];
//you can call the method now
}
}
}

how to make multi touch enabled ios app

I have made an app and i want to make it multi touch compatible. i have tried looking around but the answers aren't specific to mine.
Here is what i do:
1) My coding:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesMoved:touches withEvent:event];
if (gameState == kGameStatePaused) {(gameState = kGameStateRunning);}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(gameState == kGameStateRunning) {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
if(location.x > 400) {
CGPoint yLocation = CGPointMake(playerPaddle.center.x, location.y);
playerPaddle.center = yLocation;
}
if (gameStyle == kGameStyleTwoP) {
if(location.x < 100) {
CGPoint yLocation2 = CGPointMake(computerPaddle.center.x, location.y);
computerPaddle.center = yLocation2;
}
}
}
2) I have gone onto Interface Builder and checked the box for multitouch enabled
3) I build and run my app and it opens properly and when i go to test the multitouch i hold "option key" and click and move the mouse
4) (i am trying to make computerPaddle and playerPaddle both move) but only one of the works at a time
I have to tried to fix it but i can't understand where i am going wrong.
Any help is useful.
THX.
There is a property named multipleTouchEnabled on the UIView that you can set to YES to enable it (defaults NO).
Also, you should loop to treat all touches in the touches set that you receive in touchesMoved.
look this line
UITouch *touch = [[event allTouches] anyObject];
you only take one touch and ignore rest and thats why only one thing can move
so replace with a for loop should fix the problem
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(gameState == kGameStateRunning) {
for (UITouch *touch in [event allTouches]) {
CGPoint location = [touch locationInView:touch.view];
if(location.x > 400) {
CGPoint yLocation = CGPointMake(playerPaddle.center.x, location.y);
playerPaddle.center = yLocation;
}
if (gameStyle == kGameStyleTwoP) {
if(location.x < 100) {
CGPoint yLocation2 = CGPointMake(computerPaddle.center.x, location.y);
computerPaddle.center = yLocation2;
}
}
}
}

iPhone - ignoring the second touch on an area

I have a view that the users are allowed to finger paint. The code is working perfectly if the area is touched with one finger. For example: I touch it with one finger and move the finger. Then, a line is drawn as I move the first finger. If I touch with a second finger the same view, the line that was being drawn by the first finger stops.
I would like to ignore any touch beyond the first, i.e., to track the first touch but ignore all others to the same view.
I am using touchesBegan/moved/ended.
I have used this to detect the touches
UITouch *touch = [[event allTouches] anyObject];
lastPoint = [touch locationInView:myView];
I have also tried this
lastPoint = [[touches anyObject] locationInView:myView];
but nothing changed.
How do I do that - track the first touch and ignore any subsequent touch to a view?
thanks.
NOTE: the view is NOT adjusted to detect multiple touches.
A given touch will maintain the same memory address as long as it is in contact with the screen. This means you can save the address as an instance variable and ignore any events from other objects. However, do not retain the touch. If you do, a different address will be used and your code won't work.
Example:
Add currentTouch to your interface:
#interface MyView : UIView {
UITouch *currentTouch;
...
}
...
#end
Modify touchesBegan: to ignore the touch if one is already being tracked:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if(currentTouch) return;
currentTouch = [touches anyObject];
...
}
Modify touchesMoved: to use currentTouch instead of getting a touch from the set:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(!currentTouch) return;
CGPoint currentPoint = [currentTouch locationInView:myView];
...
}
Modify touchesEnded: and touchesCancelled: to clear currentTouch, but only if currentTouch has ended or been cancelled.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if(currentTouch && currentTouch.phase == UITouchPhaseEnded) {
...
currentTouch = nil;
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
if(currentTouch && currentTouch.phase == UITouchPhaseCancelled) {
...
currentTouch = nil;
}
}
yourView.multipleTouchEnabled = NO;
From the reference documents on UIView
multipleTouchEnabled
A Boolean value that indicates whether
the receiver handles multitouch
events.
#property(nonatomic, getter=isMultipleTouchEnabled) BOOL
multipleTouchEnabled Discussion
When set to YES, the receiver receives
all touches associated with a
multitouch sequence. When set to NO,
the receiver receives only the first
touch event in a multitouch sequence.
The default value of this property is
NO.
Other views in the same window can
still receive touch events when this
property is NO. If you want this view
to handle multitouch events
exclusively, set the values of both
this property and the exclusiveTouch
property to YES.

a question about multitouch

I have several uiimages that I can move around on the iphonescreen using multitouches. The thing is that I want to separate them in two "teams" , a "team" of uiimages that I move inside an area of my choice and a "team" that I can move all over the screen.
My question is how to use the touch methods (touchesbegan, touchesended, touchesmoved) for both of the two uiimage "teams" and their cgpoints without the cgpoints from both "teams" crossing each other and giving wrong uiimages wrong positions on the screen. the 1st "team" uses the touchesbegan, touchesmoved and touchesended methods. the 2nd "team" only uses the touchesended method.
Here´s my code. I hope that the 2 "teams" don´t cross with each other in the touchesended method
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//1st team
for(UITouch *touch in touches){
// Send to the dispatch method, which will make sure the appropriate subview is acted upon
[self getRubyAtPoint:[touch locationInView:self.view]forEvent: nil];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"touchesEnded");
//1st team
// Enumerates through all touch object
for (UITouch *touch in touches) {
// Sends to the dispatch method, which will make sure the appropriate subview is acted upon
[self dispatchTouchEndEvent:[touch view] toPosition:[touch locationInView:self.view]];
}
//2nd team
UITouch *touch = [touches anyObject];
CGPoint currentTouch = [touch locationInView:self.view];
[self getPieceAtPoint:currentTouch];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
//1st team
NSLog(#"touchesMoved");
// Enumerates through all touch objects
for (UITouch *touch in touches) {
// Send to the dispatch method, which will make sure the appropriate subview is acted upon
[self dispatchTouchEvent:[touch view] toPosition:[touch locationInView:self.view]];
}
}
If I understand, you are asking how to keep the methods such as getPieceAtPoint from acting on the wrong team.
I think I'd just add a unique tag range to each team and check that before acting on it.
Something like:
UITouch *touch = [touches anyObject];
if ([touch view].tag>=TEAM_TWO_BASE_TAG)
{
CGPoint currentTouch = [touch locationInView:self.view];
[self getPieceAtPoint:currentTouch];
}

Handling touches on Iphone

Im having a little problem on handling touches in my apps.
I set my touchesBegan like this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *currentTouch = [[event allTouches] anyObject];
touchPoint = [currentTouch locationInView:self.view];
if (CGRectContainsPoint(image1.frame, touchPoint)) {
image1IsTouched = YES;
}
}
Then i set my touch move like this:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *currentTouch = [[event allTouches] anyObject];
currentPoint = [currentTouch locationInView:currentTouch.view];
if(image1IsTouched == YES) {
image1.center = CGPointMake(currentPoint.x,currentPoint.y);
.....
}
}
Now i tried my app on actual unit and thats where i notice my problem. While im touching the image1 with 1 finger the app is doing ok and its checking for collision everytime i drag my finger. The problem occurs when i touch the screen with another finger while touching/dragging the image. The image im currently touching will jump to the other finger. I've tried [myView setMultipleTouchEnable:NO]; & using NSArray on touches and comparing the [touches count] with the touch but its not working. Can someone show me how to set a uiimageview to act on single touch only. Thanks.
First, you should use UITouch *currentTouch = [touches anyObject]; to get the current touch.
Second, you should check that touches.count == 1 to make sure there's only one finger on the screen, and ignore touch input if there's more than one, unless you wanted to support multitouch.