I have an application in which i have to move images, problem is arising when mouse touches the images it gets moved but if it touches the main View, whole view is also moving.
I tried view.userInteractionEnabled=NO;
but after one move whole view gets freeze.
I want my view to be static(not moving)
Help !!!
here is the code
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
UIImageView *imgPimples = [[UIImageView alloc]init];
imgPimples = (UIImageView*)[touch view];
CGPoint touchLocation = [touch locationInView:imgPimples];
imgPimples.center = touchLocation;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
Just check whether touched class is of kind UIImageView.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([[touch view] isKindOfClass:[UIImageView class]]) {
UIImageView *imgPimple;
imgPimple = (UIImageView *)[touch view];
CGPoint touchLocation = [touch locationInView:imgPimple];
imgPimple.center = touchLocation;
}
}
I think it will serve your purpose.
if ([touch.view isKindOfClass:[UIImageView class]]){
imgPimples.center = touchLocation;
}
assuming your parent view is not also a UIImageView.
Related
While dragin multi objects using the following code the the object is shaking when i touch the object,
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if ([touch view] == img1)
{
CGPoint location = [touch locationInView:touch.view];
img1.center = location;
return;
}
if (touch.view == img2)
{
CGPoint location = [touch locationInView:touch.view];
img2.center = location;
return;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}
What is problem in my code, is there any other method to drag and if the view contain one more object?please help me to solve this issue
I think if you are talking about more object to drag all at once then you should remove the return from you touch begain method
I have one view in iPhone application. In that view i added two UIImageView lets say img_view1, img_view2. Now i put touch event on that view as following.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Some code are here
}
how do i can get which image view began to touch ?
UITouch *touch = [touches anyObject];
CGPoint pt = [touch locationInView:self];
if (CGRectContainsPoint(img_view1.frame, pt)
{
NSLog(#"Image View 1 touched");
}
etc.
Make your image view user interaction enabled in the xib (or through the code if you are adding it programmatically) and use the UIResponder methods touchesBegan, touchesMoved, touchesEnded etc to detect the touch on the image view:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == yourImageView)
{
//add your code for image touch here
}
}
Use this code
UITouch *touch = [touches anyObject];
if (touch.view==imageView1) {
// do something
}
else if (touch.view==imageView2) {
// do something else
}
Hope this is what you are looking for.
Enjoy coding :)
use this code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if(touch.view == img_view1)
{
}
else if(touch.view == img_view2)
{
}
}
When touch began-it shows the image,when touchmoved the image follows the touch events, when touch ended the image disappear
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
[super touchesBegan:touches withEvent:event];
newgicg.hidden = NO;
NSArray *allTouches = [touches allObjects];
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
int count = [allTouches count];
if (count == 1) {
newgicg.center = touchLocation;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
newgicg.center = touchLocation;
[self.view addSubview:newgicg];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
newgicg.hidden = YES;
}
I want something like ,to follow the collection of stars when the cursor moved on the screen.i want the stars to be blinks and vanished wherever the cursor goes on the screen
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
[super touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"simple-apple.png"]];
imageView.center = touchLocation;
[self.view addSubview:imageView];
[imageView release];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
for (UIView *view in [self.view subviews]) {
[view removeFromSuperview];
}
}
try this code instead, just replace the name of the image.
EDIT:
one thing I forgot to mention is that you can keep a counter to the no. of subviews added this way and remove them , otherwise it may also remove other views which were there on the view before.
I want to create a draggable tableview in iphone,i have two tableview in a view-controller,one is ordinary tableview,but i want second tableview with draggable function,i just want to drag the tableview from bottom to center of the view.I know how to drag a image in a view controller,But i need help to create a draggable tableview.Please help me.
Thanks in advance.
#interface UITouchTutorialViewController : UIViewController {
IBOutlet UIImageView *img;
}
#end
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
img.center = location;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}
#interface UITouchTutorialViewController : UIViewController {
IBOutlet UITableView *table;
}
#end
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
table.center = location;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}
Is there a way to tell if the touch is currently moving? If so could someone lead me to the right place?
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
//Here urSprite is your CCSprite which you want to move
CGRect urSpriteRect = CGRectMake([urSprite position].x - [urSprite contentSize].width/2,
[urSprite position].y - [urSprite contentSize].height/2,
[urSprite contentSize].height, [urSprite contentSize].width);
if (CGRectContainsPoint(urSpriteRect, location))
{
checkFlag = TRUE;
NSLog(#"You Touched the sprite!");
}
}
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(checkFlag){
urSprite.position = location;
NSLog(#"You are moving your sprite!");
}
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
checkFlag = FALSE;
NSLog(#"Sprite stopped!");
}
If your layer has touches enabled, you can implement the following method in your layer to handle move events for touches...
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event