正しい方向に私を導くためのrobmayoffによる大きな助け。
- - - - - - - - - 編集: - - - - - - -
注: これは、アプリを削除しているときに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」メソッドに適切なメッセージを表示し、そこで削除コードを処理します。