How to drag and move UITextFields? - objective-c

I have it set up where i can move multiple labels. I'm having problems moving a UITextfield. I made sure that user interaction and multiple touch is enabled.
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if ([touch view] == tex1) {
tex1.center = location;

Try to make this trick, it works for me
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *clearView = [[UIView alloc] initWithFrame:tex1.frame];
[clearView setTag:101];
[self.view addSubview:clearView];
[clearView release];
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([[touch view] tag] == 101 && [touch tapCount] == 2) {
[tex1 becomeFirstResponder];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if ([[touch view] tag] == 101) {
tex1.center = location;
[[touch view] setCenter:location];
}
}

Related

Carrying a touch event from beginning to end in Sprite Kit

I am trying to create an app that creates a sprite node when the touch begins, drags it when the user's finger moves, and drops it when the touch has ended. What I have now simply creates a new sprite for every new touch location:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
SKSpriteNode *gamePiece = [self pickObject];
gamePiece.position = location;
gamePiece.physicsBody.dynamic = NO;
[self addChild:gamePiece];
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
SKSpriteNode *gamePiece = [self pickObject];
gamePiece.position = location;
gamePiece.physicsBody.dynamic = NO;
[self addChild:gamePiece];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
SKSpriteNode *gamePiece = [self pickObject];
gamePiece.position = location;
gamePiece.physicsBody.dynamic = NO;
[self addChild:gamePiece];
}
}
How do I carry the same, single sprite node across the touch methods? I have searched far and wide for tutorials, but nothing seems to fit exactly what I need.
You can do so by maintaining an instance variable for the touch as well as the SKSpriteNode associdated with that touch.
#implementation MyScene
{
UITouch *currentTouch;
SKSpriteNode *currentGamePiece;
}
Then, modify the touch delegates as follows:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
SKSpriteNode *gamePiece = [self pickObject];
gamePiece.position = location;
gamePiece.physicsBody.dynamic = NO;
[self addChild:gamePiece];
currentTouch = touch;
currentGamePiece = gamePiece;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
if ([touch isEqual:currentTouch])
{
currentGamePiece.position = location;
}
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
for (UITouch *touch in touches)
{
CGPoint location = [touch locationInNode:self];
if ([touch isEqual:currentTouch])
{
currentGamePiece.position = location;
currentGamePiece = nil;
currentTouch = nil;
}
}

crashed when touch two sprites at the same time(cocos2d-iphone)

The app will crash when two sprites are touched at the same time.
-(void)addEnemy
{
enemy = [CCSprite spriteWithFile:#"enemy.png"];
enemy.position = ccp(winsize.width / 2, winsize.height / 2);
[spriteSheet addChild:enemy];
[spritetiles addObject:enemy]; //spritetiles is NSMutableArray
}
touch code
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [self convertTouchToNodeSpace: touch];
for (CCSprite *target in [spriteSheet children]) {
if (CGRectContainsPoint(target.boundingBox, location)) {
[target stopAllActions];
[spriteSheet removeChild:target cleanup:YES];
[spritetiles removeObject:target];
}
}
}
if I touch any one of the sprite, there's no error, but if i touch two sprites(sometime some sprites' position is nearby), the app will crash, at the code line "if (CGRectContainsPoint(target.boundingBox, location)) {", so how can I fix it? thanks
Updated
Use reverseEnumerator in order to iterate through an array when you may need to remove elements as part of the for loop:
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [self convertTouchToNodeSpace: touch];
for (CCSprite *target in [spriteSheet.children reverseObjectEnumerator]) {
if (CGRectContainsPoint(target.boundingBox, location)) {
[target stopAllActions];
[spriteSheet removeChild:target cleanup:YES];
[spritetiles removeObject:target];
}
}
}

How to find out how many times a user touched an image

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchlocation = [touch locationInView:self.view];
How would you know when a user touched a certain button and how many times if i have four different buttons.
I'm into C# so forgive me if I'm wrong, but couldn't you just increase an integer whenever the user clicks on an image, then if you wanted to know the amount of clicks, you'd look at this integer?
what you will have to do is take one integer in.h file then in .m file write touchesbegan method like below.
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchlocation = [touch locationInView:self.view];
if([touch isKindOfClass:[UIImage class]])
{
myInt++;//your global integer....
}
}
let me know it is working or not!!!
Happy Coding!!!!
Try to use this one..
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
// ----------you can find using tag and first set button's tag in xib ------
if(touch.view.tag == btnRed.tag)
{
redTouch++:
}
}

cocoa touch hitTest withEvent can recognize UIView subclass

I have a view with subViews, these subViews are a subClass of UIView, in the example the subClass is called ESDcelda
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIImage *img = [UIImage imageNamed:#"lgrey091.gif"];
[self setBackgroundColor:[UIColor colorWithPatternImage:img]];
ESDcelda *cel1 = [[ESDcelda alloc] initWithTipo:1];
[cel1 setFrame:CGRectMake(100, 100, cel1.frame.size.width, cel1.frame.size.height)];
[self addSubview:cel1];
cel1 = [[ESDcelda alloc] initWithTipo:2];
[cel1 setFrame:CGRectMake(300, 100, cel1.frame.size.width, cel1.frame.size.height)];
[self addSubview:cel1];
}
return self;
}
now I'm triyng to know what kind of UIView I am pointing with the touchEvents with the following methods but in the log the pointer "vista" only recognizes the self class or the UIView class, there is any way to recognize the subClass "celdaSel" ?
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[touches allObjects] objectAtIndex:0];
[self perfTouch:touch];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[touches allObjects] objectAtIndex:0];
[self perfTouch:touch];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[touches allObjects] objectAtIndex:0];
[self perfTouch:touch];
}
-(void)perfTouch:(UITouch *)touch
{
CGPoint punto = [touch locationInView:self];
UIView *vista = (ESDcelda *)[self hitTest:punto withEvent:nil];
if (![vista isKindOfClass:[self class]])
{
celdaSel = (ESDcelda *)vista;
[celdaSel seleccion:YES];
}
else
{
if (celdaSel != nil)
{
[celdaSel seleccion:NO];
}
}
}
Solved, there are the steps
In the main view only left the code that interpects the touch and where is it.
v
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[touches allObjects] objectAtIndex:0];
[self perfTouch:touch];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[touches allObjects] objectAtIndex:0];
[self perfTouch:touch];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[touches allObjects] objectAtIndex:0];
[self perfTouch:touch];
if (self.celdaSel != nil)
{
NSLog(#"%d",self.celdaSel.elemento);
}
}
-(void)perfTouch:(UITouch *)touch
{
CGPoint punto = [touch locationInView:self];
[self hitTest:punto withEvent:nil];
}
In the UIView subClass called ESDCelda I override pointInside:withEvent method to know if the current single touch is on the view, "inter" is the variable that lets me know if the touch is on the view, "seleccionada" indicates if the view is highlighted, "conten" is a pointer to the superview, and "seleccion:" is the method that highlights the view it self.
v
-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
BOOL inter = [super pointInside:point withEvent:event];
if (inter)
{
//NSLog(#"%i, %#", inter, self);
if (!self.selecionada)
{
[self seleccion:YES];
if (self.conten.celdaSel != nil)
[self.conten.celdaSel seleccion:NO];
[self.conten setCeldaSel:self];
}
}
else
{
if (self.selecionada)
{
[self seleccion:NO];
[self.conten setCeldaSel:nil];
}
}
return inter;
}

how to let the touchesMoved only control one view?

i create an UIButton on the view,and i want to let the touchesMoved only control the UIButton,not the whole view
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint touchMoved = [touch locationInView:self.view];
}
i want to do like this
if i touch the UIButton,and then the UIButton can be moved with my finger,if i touch other view and my finger is moving on the screen,the UIButton does nothing.
that means the function touchesMoved only has the role of the UIButton,so how can i do it? thanks
I presume the code you show takes place in your custom view controller subclass, and the UIButton is a subview of its view.
Define a simple BOOL in your class that you set to NO first. Then update it in the event handling methods.
// .h
BOOL buttonTouched;
// .m
// in the viewDidLoad
buttonTouched = NO;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// test wether the button is touched
UITouch *touch = [touches anyObject];
CGPoint touchBegan = [touch locationInView:self.view];
if(CGRectContainsPoint(theButton.frame, touchBegan) {
buttonTouched = YES;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(buttonTouched) {
// do it here
UITouch *touch = [touches anyObject];
CGPoint touchMoved = [touch locationInView:self.view];
CGRect newFrame = CGRectMake(touchMoved.x,
touchMoved.y,
theButton.frame.width,
theButton.frame.height);
theButton.frame = newFrame;
}
}
// when the event ends, put the BOOL back to NO
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
buttonTouched = NO;
}