rob mayoff的大力帮助引导我朝着正确的方向前进。
- - - - - - - - - 编辑: - - - - - - -
注意: 在删除任何应用程序时,这不会像在 iPhone 中那样精确显示,但仍然可以很容易地实现,因为它以不同的方式制作动画。
------------------
我通过以下方式为我的代码实现了删除功能:
-(void)handleLongPress:(UILongPressGestureRecognizer *)swipe
{
if (swipe.state == UIGestureRecognizerStateBegan)
{
NSLog(@"long pressed up..");
@try {
UIButton *btnDelete = (UIButton *)[self.view viewWithTag:977];
[btnDelete removeFromSuperview];
if(self.btnIDToDelete == swipe.view.tag)
{
self.btnIDToDelete = -1;
UIButton *btnToDelete = (UIButton *)[self.view viewWithTag:swipe.view.tag];
CALayer *layer = btnToDelete.layer;
[layer removeAnimationForKey:@"DeleteAnimation"];
return;
}
}
@catch (NSException *exception) {
}
@finally {
}
NSInteger tag = swipe.view.tag;
NSLog(@"id is = %d", tag);
self.btnIDToDelete = tag;
UIButton *btnToDelete = (UIButton *)[self.view viewWithTag:tag];
UIButton *btnDelete = [[UIButton alloc] initWithFrame:CGRectMake(btnToDelete.frame.origin.x - 1, btnToDelete.frame.origin.y + 45, 50, 49)];
[btnDelete addTarget:self action:@selector(deleteFavButton:) forControlEvents:UIControlEventTouchUpInside];
btnDelete.tag = 977;
UIImage *imgBack = [UIImage imageNamed:@"crossIpad.png"];
[btnDelete setBackgroundImage:imgBack forState:UIControlStateNormal];
[self.view addSubview:btnDelete];
[btnDelete release];
CABasicAnimation *pulseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
pulseAnimation.duration = .5;
pulseAnimation.toValue = [NSNumber numberWithFloat:1.1];
pulseAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pulseAnimation.autoreverses = YES;
pulseAnimation.repeatCount = FLT_MAX;
//pulseAnimation.repeatCount = 5;
//pulseAnimation.fillMode = kCAFillModeForwards;
//pulseAnimation.removedOnCompletion = NO;
pulseAnimation.fillMode = kCAFillModeBackwards;
pulseAnimation.removedOnCompletion = YES;
float xVal = btnToDelete.frame.origin.x;
float yVal =btnToDelete.frame.origin.y;
float widthVal = btnToDelete.frame.size.width;
float heightVal = btnToDelete.frame.size.height;
NSLog(@"-- xVal=%f -- yVal=%f -- widthVal=%f -- heightVal=%f -- ",xVal, yVal, widthVal, heightVal);
if(xVal < 48)
xVal = 48;
else if (xVal > 250 && xVal < 278)
xVal = 278;
else if (xVal > 480 && xVal < 508)
xVal = 508;
if(yVal < 95)
yVal = 95;
else if(yVal > 250 && yVal < 310)
yVal = 310;
else if(yVal > 500 && yVal < 525)
yVal = 525;
[btnToDelete setFrame:CGRectMake(xVal, yVal, widthVal, heightVal)];
CALayer *layer = btnToDelete.layer;
[layer addAnimation:pulseAnimation forKey:@"DeleteAnimation"];
}
}
你应该这样称呼它:
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];
longPress.numberOfTouchesRequired = 1;
[btn addGestureRecognizer:longPress];
[longPress release];
注意:在“deleteFavButton”方法中显示适当的消息并在那里处理删除代码。