Objects on screen start blinking when dragging objects - iphone

I have an object that the user can drag around the screen in my app. But for some reason, when they start to drag it over another UIView, the items on screen start blinking. So part of the CAGradientLayer will appear in front of everything else, some things will seem to push themselves to the back, all sorts of bizarre activity. I haven't been able to take a screenshot of this unfortunately. This is the code that i've been using to drag the dragging.
NSSet *touches = [event touchesForView:sender];
UITouch *myTouch = [touches anyObject];
CGPoint startPoint = [myTouch locationInView:self.view];
positionX = startPoint.x;
positionY = startPoint.y;
colourDropView.center = CGPointMake(positionX-15, positionY-25);
colourDropView is the object that's being dragged, as you might guess. And it's when that last line is implemented when it starts the blinking, and it happens each time the user moves their finger. No other code is running when they drag their finger, only what is above.
Any ideas as to why this may be happening?

Turns out it's a bug. colourDropView had rounded corners, and had 'maskstobounds' set to YES. For some reason this causes a CAGradientLayer to freak out when this view is dragged over it.

Related

restart an uitouch

I've tried all morning, and start to think it's impossible.
So you have a touch in your screen, it entered the response chain and maybe did something. Then you moved it on the screen it called the touchesMoved: method (of the uiResponder associated with the touch) and maybe did other stuff.
At this point, is there anyway, that the touch continues to move in the screen, and when it enters some other uiView, the uiTouch gets reinitialized (I mean, call touchEnded, in the first uiView, then in the new uiView call touchesBegan, and then if continues movement call touchesMoved).
I've tried to resignFirstResponder and to manually call the methods, and I just can't.
I keep thinking that there should exist a really simple answer for this. Something like
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.superview];
if (!CGRectContainsPoint(self.frame, location)) {
[touch restart];
}
or
[[touch phase] restart];
(But anything in documentation ):
My recommendation would be to have the superview of all the views you want to transition through to receive the touches, then do a hit test on the subviews within the touchesMoved/Began/Ended methods (probably via CGRectContainsPoint(...)), and pass the touches along accordingly (calling the corresponding touchesBegan/Moved/Ended method on the subview).

how to move view with my finger in iphone Objective-c

i have this view that the size of him is 1280 X 345 and im moving it left and right in my screen.
now, my main question is how do i make it move with my finger (not swipeLeft / swipeRight) i want it to move with my finger like the home screen of the iPhone IOS.
now, im using this code:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:infoView];
//[self.view setTransform:CGAffineTransformTranslate(CGAffineTransformIdentity, (location.x + self.view.frame.origin.x - dx), 0.0)];
NSLog(#"touch: %f",location.x);
infoView.frame = CGRectMake(infoView.frame.origin.x + location.x, 0, 1280, 345);
}
that should be the current way but i cant figur it out.
i have also try to find an answer in google and here, but, as you know.. i didn't found something useful.
i've also made this so you can understand it better.
Don't write code to do this yourself. Use UIScrollView. This is what it is designed for.
If you want to have the same effect as the iPhone/iPad home screen you should use a UIScrollView and enable paging.
If you really want to handle it yourself for some reason, avoiding UIScrollView, there are UIGestureRecognizers, specifically UIPanGestureRecognizer which can leverage most of the job handling multiple touch events in one place.
i used the UIPagecontrol and UIScrollView to make it. very easy and smart object !

Cocos2d-iPhone Drag anywhere to move sprite

Ok what I would like to do is duplicate the controls of Space Invaders Evolution.
Begin a touch anywhere then drag around and the sprite moves 1:1 with your finger.
Sprite can not be moved of screen.
I have been using UIGestureRecognizer to handle dragging the sprite around the screen but im still pretty new to this and I have not been able to get this to work yet.
I don't know if you're unwilling to try this but it seems to me that this sort of thing could easily be done using ccTouchesBegan and ccTouchesMoved. I'd implement it by putting the following in my ccTouchesBegan and ccTouchesMoved methods (where sprite is the name of the sprite you're trying to move):
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
sprite.position = ccp(location.x, location.y);
Are you using Cocos2D or the Apple Frame-work?
In cocos2d you could use a layer (your scene for example) to detect the touch, which then moves the sprite. When the move is applied, check whether the sprite is still within screen boundaries, and move it back into screen boundaries if it's not.
There is example touch code in the cocos2d distribution. Not for your specific case, but general code, with text and explanation.

How to center align a sprite?

In cocos2d does anyone know how to center a sprite? Right now, I have a sprite that moves to where you touch on the screen. The problem is that the sprite is aligned to the lower left corner. This means that instead of moving down, if you touch just a little over the bottom the sprite will move up. Thanks in advance!
Here is my code...
(BOOL) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *) event {
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView: [touch view]];
[mainSprite do:[MoveTo actionWithDuration:0.5 position:ccp(point.x, 480 - point.y)]];
return YES;
}
The default transformation anchor for sprites in Cocos2D is the center of the sprite, so it should be moving such that the center of the sprite ends up in the touched location as you have it now. Have you changed the sprite's transform anchor?
The only other thing I can think of is that if your mainSprite is a child of another CocosNode then you may need to convert the touch coordinates to node space using this method:
- (CGPoint)convertToNodeSpace:(CGPoint)worldPoint;
...on the parent node. However, I doubt that is the problem. Sorry if this is unhelpful.
EDIT: OP, if you read this, what version of Cocos2D are you using? I believe 0.8 (currently in the svn trunk) changes the way that anchoring works; for future reference it may be useful to others to know what you're working with.
I got it working! For anyone that wants to know here is the code...
[mainSprite setTransformAnchor:ccp(24.0, 64.5)];
24 is half of the sprites width
64.5 is half of the sprites height

What happens when dragging off screen?

I have an object that can be dragged around. Once the user's finger goes off screen and comes back, I loose the ability to drag the object. If the user then does another touch and drag, everything is fine.
How can I get notified once the user's finger drags back onto the screen? Since touchesBegan doesn't fire, I don't get any notification.
Here is my touchesMoved, which I call in the touchesBegan:
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
//stop object dragging at edge of screen
if(location.x > 35){
myboject.center = location;}
}
The described behaviour seems normal to me, and all built-in Apple apps behave the same way. Since there’s no touch screen outside of the touch screen (yep), I believe there’s no way the device can distinguish between touch beginning or moving from outside of the screen.