0

I am trying to implement ,long press and delete functionality of apple, myself.

I almost achieved but, there is something i could not figure out.

When shaking animation starts delete button do not take the touches.

How to handle while it is shaking deleting the view.

Here is my code;

    self.frame = CGRectMake(0, 0, 1024, 768);

    [self setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.8]];


    circleView = [[UIView alloc] initWithFrame:CGRectMake(50,55,90,90)];
    circleView.layer.cornerRadius = 45;

    circleView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1];
    circleView.layer.borderColor = [[UIColor blackColor] CGColor];
    circleView.layer.borderWidth = 4;

    UILabel* circleIndex = [[UILabel alloc] init];
    circleIndex.frame    = CGRectMake(30, 25, 40, 40);
    [circleIndex setFont:[UIFont fontWithName:@"Arial-BoldMT" size:40]];
    [circleIndex setText:@"A"];

    [circleView addSubview:circleIndex];



    UIView *exitView = [[UIView alloc] initWithFrame:CGRectMake(78,5,20,20)];
    exitView.layer.cornerRadius = 10;

    exitView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:1];
    exitView.layer.borderColor = [[UIColor whiteColor] CGColor];
    exitView.layer.borderWidth = 2;

    UILabel* exitLabel = [[UILabel alloc] init];
    exitLabel.frame    = CGRectMake(5, 0.5, 25, 20);
    [exitLabel setFont:[UIFont fontWithName:@"Arial-BoldMT" size:25]];
    [exitLabel setTextColor:[UIColor whiteColor]];
    [exitLabel setBackgroundColor:[UIColor clearColor]];
    [exitLabel setText:@"-"];

    [exitView addSubview:exitLabel];


    [circleView addSubview:exitView];

    UIView *alertView  = [[UIView alloc]initWithFrame:CGRectMake(40, 80, 944, 600)];        

    [exitView setUserInteractionEnabled:YES];


    UILongPressGestureRecognizer *longPress =
    [[UILongPressGestureRecognizer alloc] initWithTarget:self
                                                  action:@selector(handleLongPress:)];
    [circleView addGestureRecognizer:longPress];


    UITapGestureRecognizer *singlePress =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                                  action:@selector(handleSinglePress:)];
    [exitView addGestureRecognizer:singlePress];
    //[longPress release];

    [circleView bringSubviewToFront:exitView];

    [alertView addSubview:circleView];

    [self addSubview:alertView];

}
return self;
}




//The event handling method
- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer{
//CGPoint location = [recognizer locationInView:[recognizer.view superview]];
if ( recognizer.state == UIGestureRecognizerStateEnded ) {

    //[circleView removeFromSuperview];

    [self shakeView:recognizer.view];
}


 }

- (void)handleSinglePress:(UITapGestureRecognizer *)recognizer{
//CGPoint location = [recognizer locationInView:[recognizer.view superview]];
if ( recognizer.state == UIGestureRecognizerStateEnded ) {

    [recognizer.view.superview removeFromSuperview];

    }


 }

  - (void)shakeView:(UIView *)viewToShake
  {
CGFloat t = 2.0;
CGAffineTransform translateRight  = CGAffineTransformTranslate(CGAffineTransformIdentity, t, 0.0);
CGAffineTransform translateLeft = CGAffineTransformTranslate(CGAffineTransformIdentity, -t, 0.0);

viewToShake.transform = translateLeft;

[UIView animateWithDuration:0.07 delay:0.0 options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat animations:^{
    [UIView setAnimationRepeatCount:200.0];
    viewToShake.transform = translateRight;
} completion:^(BOOL finished) {
    if (finished) {
        [UIView animateWithDuration:0.05 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
            viewToShake.transform = CGAffineTransformIdentity;
        } completion:NULL];
    }
}];
}
madLokesh
  • 1,860
  • 23
  • 49
erdemgc
  • 125
  • 1
  • 9

1 Answers1

0

Use in the options parameter of your animation UIViewAnimationOptionAllowUserInteraction

Chris Tetreault
  • 1,973
  • 13
  • 19