Reputation: 1390
Here I write the code to move the imageView on click. I want to perform move up on first click and if it is clicked again it goes back. Here's my code :
-(void)gdown
{
if (penview.center.y > 428) penview.center = CGPointMake(penview.center.x, penview.center.y -5);
if(penview.center.x ==428)
{
[movtimer invalidate];
}
}
-(void)buttonmover
{
movtimer=[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(gdown) userInfo:nil repeats:YES];
if(movtimer==nil)
{
movtimer=[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(gdown) userInfo:nil repeats:YES];
}
}
-(void)gdup
{
if (penview.center.y < 480) penview.center = CGPointMake(penview.center.x, penview.center.y +5);
if(penview.center.y ==480)
{
[movtimers invalidate];
}
}
-(void)buttonmovup
{
movtimers=[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(gdup) userInfo:nil repeats:YES];
if(movtimers==nil)
{
movtimers=[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(gdup) userInfo:nil repeats:YES];
}
}
The method performing the click operations :
-(IBAction)popupview:(id)sender
{
UIButton *button = sender;
if(button.selected) //
{
[self buttonmover];//gooin up
button.selected = false;
}
else
{
[self buttonmovup];//button goin down
button.selected = true;
}
}
Its not working. The image view shakes while clicking. What change should I make in this method?
Upvotes: 0
Views: 94
Reputation: 1616
Your code is right.Only do following change
movtimers=[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(gdown) userInfo:nil repeats:NO];
for every timer set repeat to NO instead of YES
Ok if you want to show animation then change your if statement like this
//in gdown
if(penview.center.y <=428)
{
[movtimers invalidate];
}
//in gup
if(penview.center.y >=480)
{
[movtimers invalidate];
}
Upvotes: 0
Reputation: 1616
Your code is right.Only do following change
movtimers=[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(gdown) userInfo:nil repeats:NO];
for every timer set repeat to NO instead of YES
Upvotes: 0
Reputation: 4805
I don't think your else part code is getting executed. You should maintain
a flag
in your class for the same as selection property will always be true when button is tapped.
Upvotes: 1