I got an Icon of UIImageView named IconView. I want to make sure when I touch and move this Icon, its position only change inside the boundary of another UIImageView named backgroundView.
I thought after I add the IconView as the subview of backgroundView, the boundary is automatically set. But it seems to be wrong.
[backgroundView addSubview:IconView];
after this, I can still move the Icon to the outside of backgroundView.
how can I set the limitation? thanks.
You try like this,
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if(CGRectContainsPoint(urBoundryBackgroundImage.frame, location)) {
//do your moving stuff
}
}
Related
I would like the user to be able to press and drag one image (ex: image1.png) and place it over another (ex: image2.png). When the user releases, a third image (ex: image3.png) is added to the screen. How could I go about doing that in xcode?
It's not so easy to do what you want, you have to start to see this touches tutorial from Apple Developer program
here is a code sample for the image moving:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if (CGRectContainsPoint([image1 frame], location)) {
images.center = location;
if (CGRectContainsPoint([image1 frame], [image2 frame]) {
//place your code when image1 = images2
[self.view addSubview:image3];
}
}
}
i think something like this could work
I m new to game devloping in ios. Now I want made a game similar like "Control Air Flight" and "Air Traffic Controller" ,
where a user can draw line using their finger and an object will
follow that path
so ,anyone can guide me which is best for developing like this.Can Cocos2d is best for it? Or any thing else i have to use for this.
Also If anyone knows of a tutorial already out there or any reference link please suggest me.
Thanks in Advance.
To simply let the object follow your finger, implement touches (and just one of its methods):
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint toPoint = [touch locationInView:self.view];
[yourObjectOutlet setCenter:toPoint];
}
Here, your object's center will follow your path, but you can adjust its anchor point by editing "toPoint" accordingly to object's frame.
EDIT
If you want to draw the path, then make the object follow that path, do like this:
//define an NSMutableArray in your header file (do not forget to alloc and init it in viewDidLoad), then:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//you begin a new path, clear the array
[yourPathArray removeAllObjects];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint toPoint = [touch locationInView:self.view];
//now, save each point in order to make the path
[yourPathArray addObject:[NSValue valueWithCGPoint:toPoint]];
}
Now you want to start the move:
- (IBAction)startMoving{
[self goToPointWithIndex:[NSNumber numberWithInt:0]];
}
- (void)goToPointWithIndex:(NSNumber)indexer{
int toIndex = [indexer intValue];
//extract the value from array
CGPoint toPoint = [(NSValue *)[yourPathArray objectAtIndex:toIndex] CGPointValue];
//you will repeat this method so make sure you do not get out of array's bounds
if(indexer < yourPathArray.count){
[yourObject setCenter:toPoint];
toIndex++;
//repeat the method with a new index
//this method will stop repeating as soon as this "if" gets FALSE
[self performSelector:#selector(goToPointWithIndex:) with object:[NSNumber numberWithInt:toIndex] afterDelay:0.2];
}
}
That's all!
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.
I recently downloaded the sample application, GLPaint from apple. This app showed me how to implement drawing, but now I would like to animate a image over the drawn lines. Similarly to how the application "Flight Control" works, I would like to be able to draw a path for an image and then have the image animate over this path.
any ideas?
you could create your path
CGMutablePathRef thePath = CGPathCreateMutable();
and then in the touchesBegan and touchesMoved add to your path
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if(touch){
CGPoint tapPoint = [touch locationInView: self.view];
CGPathMoveToPoint(thePath, NULL,tapPoint.x,tapPoint.y);
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if(touch){
CGPoint tapPoint = [touch locationInView: self.view];
CGPathAddLineToPoint(thePath, NULL,tapPoint.x,tapPoint.y);
}
}
and on touchedEnded, as Joshua said, create a CAKeyframeAnimation and set its path to thePath and set the other properties of your animation (duration, etc) and apply it to your object
Look at CAKeyframeAnimation, this allows you to set a path (which is a CGPathRef) for the animation to run along.
Based on code from AtomRiot, I created a simple project on my blog. See this post, also posted a short video showing the result.
However when the animation ends, the image will somehow repositioned back to (0,0), so might need to add extra code to reset the image location after animation finished.
Plus, found that you can't change the animation once it started, have to wait for it to end before you can change it.
Hope this helps!
I am trying to build an iPhone app by using Cocos2d. And i would like to set an image to another fixed position from a fixed position by using touch as my wish(speedy, or slowly). I have got some code but it does not work properly.
so friends it will more helpful to me if i get any solution.
The question is a little fuzzy, but if you want to set the position of a CocosNode you do:
[myNode setPosition:cpv(x,y)];
If you want the node to be offset from a touch location, you can do this by implementing ccTouchesBegan:withEvent
-(BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
CGPoint convertedLocation = [[Director sharedDirector] convertCoordinate:location];
[myNode setPosition: cpv(convertedLocation.x - 100, convertedLocation.y - 100)];
return kEventHandled;
}
That will offset the CocosNode by -100,-100 to where the touch occurred.
The ccTouchesBegan:withEvent: should be implemented in your Layer, and isTouchesEnabled should be set to YES to enable touches.