7

I have a subclass of UIView, and added the touchesBegan and touchesEnd methods...

In touchesBegan, I set the backgroundColor from white to green by using self.backgroundColor = [UIColor greenColor] ... in the touchesEnd I reset the color to white.

It works but very slowly. By tapping the view, it takes 0.5 - 1.0 sec until I see the green color.

Selecting a cell in a UITableView it's much faster.

CupawnTae
  • 14,192
  • 3
  • 29
  • 60
Maurice Raguse
  • 4,437
  • 2
  • 29
  • 46
  • hm, might be due to an implicit animation when setting properties like backGroudn color. Can you check if the touch is late or only the change of color? – Volker Aug 05 '15 at 09:46
  • This UIView of yours happens to be inside of a UITableView or a UIScrollView ? – The dude Aug 05 '15 at 09:48
  • 2
    @Volker Nice thinking, but `UIKit` disables implicit animations of underlying `CALayer`s ([see this great answer here](http://stackoverflow.com/questions/4749343/when-exactly-do-implicit-animations-take-place-in-ios)), so I guess this is not the problem. I would look for `delaysContentTouches` of a superview or something similar. – Alladinian Aug 05 '15 at 09:51
  • @Thedude -> yes, its a UITableViewCell – Maurice Raguse Aug 05 '15 at 10:12
  • I know this has already an accepted answer, but I suggest you try this out: UITableViews delay content touches by default. It has a property called delaysContentTouches, you should set it to NO. https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScrollView_Class/#//apple_ref/occ/instp/UIScrollView/delaysContentTouches – The dude Aug 05 '15 at 13:02
  • delayContentTouches = NO -> same delay :-( – Maurice Raguse Aug 06 '15 at 08:09

2 Answers2

4

Try this:

self.view.userInteractionEnabled = YES;
    UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(doCallMethod:)];
    recognizer.delegate = self;
    recognizer.minimumPressDuration = 0.0;
    [self.view addGestureRecognizer:recognizer];

- (void)doCallMethod:(UILongPressGestureRecognizer*)sender {
    if(sender.state == UIGestureRecognizerStateBegan){
        NSLog(@"Begin");
        self.view.backgroundColor = [UIColor greenColor];
    }else if (sender.state == UIGestureRecognizerStateEnded){
        NSLog(@"End");
        self.view.backgroundColor = [UIColor whiteColor];
    }
}

Note: It will work more faster.

Mayur Prajapati
  • 5,454
  • 7
  • 41
  • 70
  • ok this works but if i putmy new View in a UITableViewCell ... i'cant swipe the cell to see the remove button :-( – Maurice Raguse Aug 05 '15 at 15:34
  • Works, but if the view is inside a superView of kind UIScrollView... the ScrollView can't scroll by beginning the scroll inside the view with minimumDressDuration = 0.0 ... so you have to adjust this value to e.g. 0.1 ... and the UIScrollView is able to scroll – Maurice Raguse Aug 06 '15 at 09:44
  • 1
    why touchesBegan will have delay? anyway to speed up? – TomSawyer Nov 16 '15 at 03:09
2

You should use a gesture recognizer as TheBurgerShot suggested but I recommend you an UILongPressGestureRecognizer. Something like:

UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(changeColor:)];
gesture.minimumPressDuration = 0.f;
[self.yourView addGestureRecognizer:gesture];

in your viewDidLoad. And:

-(void) changeColor:(UIGestureRecognizer *)gestureRecognizer{

    if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
        self.yourView.backgroundColor = [UIColor greenColor];
    }
    else if (gestureRecognizer.state == UIGestureRecognizerStateEnded){
        self.yourView.backgroundColor = [UIColor whiteColor];
    }
}
Y.Bonafons
  • 2,329
  • 13
  • 20
  • do you know why touchesbegan has some delay? use gesture can solve the trouble but can't have every attributes like touchesbegan like multiple touch. – TomSawyer Nov 16 '15 at 07:25