Jump to content

CABasicAnimation Problem


13 posts in this topic

Recommended Posts

Here is my code, if I add the CABasicAnimation,

 

the UITouch *touch = [[event allTouches] anyObject]; will return to MainView not UIImageView,

 

is there other way to add the CABasicAnimation?

 

MainView.m

 

- (void)createGame:(int)numberDot
{
for(int i = 0; i <numberDot; i++) {
	float xPos = 0;
	float yPos = 60 * i;
	imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xPos, yPos, 57.0, 57.0)];
	imageView.image =[UIImage imageNamed:@"Icon8.png"];
	imageView.userInteractionEnabled = YES;
	[self addSubview:imageView];
	[imageViewArray insertObject:imageView atIndex:i]; 

	CABasicAnimation *theAnimation;
	theAnimation = [CABasicAnimation animationWithKeyPath:@"position.x"];
	theAnimation.duration = 20.0;
	theAnimation.repeatCount = 2;
	theAnimation.autoreverses = YES;
	theAnimation.fromValue = [NSNumber numberWithFloat:10.0];
	theAnimation.toValue = [NSNumber numberWithFloat:300.0];
	[[imageView layer] addAnimation:theAnimation forKey:@"animateOpacity"];

	[imageView release];
}
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{	
UITouch *touch = [[event allTouches] anyObject];
for(int i = 0; i < [imageViewArray count]; i++) 
{
	id anObject = [[imageViewArray objectAtIndex:i] retain];

	if([touch view] == anObject)
	{
		[anObject removeFromSuperview];
		[imageViewArray removeObjectAtIndex:i];
	}
}
}

Link to comment
Share on other sites

First of all, you shouldn't release imageview at the end of the first function.

 

You also shouldn't retain it when taking it from the array. Retaining takes away the pointer that was there, and makes a new copy of it, so there is no more pointer. You want a pointer and you want it to be the same one that you added, so you can remove it from superview.

 

This may be your problem

 

Hope it helps!

Link to comment
Share on other sites

I just try finding all the resource on internet, but I can't find out how to fix the UITouch + CAanimation touch problem......

 

please help........... :)

 

if there any other way to add the animation?

 

Can I use NSTime to add the animation every second?

Link to comment
Share on other sites

As long as you have the "imageView.userInteractionEnabled = YES;" method, you don't

 

Yes, you do. He's trying to customize what happens on a touch event. That requires subclassing the method.

 

Edit: well, you're right, you don't need to subclass it. But it's probably the better way to do it.

Link to comment
Share on other sites

still can't find out the solution......

 

can I use UIView beginanimations to change the position of UIImageView instep of using CAAnimation.

 

because I can use the setAlpha to change the alpha, but I can't find out how to change the postion of it.

Link to comment
Share on other sites

lamfan: use -setFrame: and pass a CGRectMake() as the argument.

 

yes I use the setFrame method of UIView, the image did move but the touch will not return the UIimageView,

 

it return the mainView again......

 

what's wrong with that?

 

 

- (void)createGame:(int)numberDot
{
for(int i = 0; i < numberDot; i++) {
	float xPos = 10;
	float yPos = 60 * i + 10;
	imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xPos, yPos, 57.0, 57.0)];
	imageView.image =[UIImage imageNamed:@"Icon8.png"];
	imageView.userInteractionEnabled = YES;

	[self addSubview:imageView];
	[imageViewArray insertObject:imageView atIndex:i]; 

	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:5.0];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
	CGRect tempFrame = [imageView frame];
	tempFrame.origin.x = 120.0f;
	[imageView setFrame:tempFrame];
	[UIView commitAnimations];
}
}

Link to comment
Share on other sites

I'm confused, you want to *return* your image view on touch? Or do you want the touch to work with your image view?

 

I just want to remove the animation UIView when you touch it, but the touch event will not return value when the UIView is in animation!

Link to comment
Share on other sites

Finally I find out a stupid way to make it work!

 

I'm using NSTimer to update the frame position of UIView,

 

so the touchesBegan event will respond.

 

That also mean I can't apply the UIView Animation and CAAinimation to my UIView........

 

so sad.......... :)

Link to comment
Share on other sites

 Share

×
×
  • Create New...