开发者

UITouchMoved and UITochBegin

I am using touchBegin and touchMoved in my app.

i am setting array of uiview in the screen:

    UIView *white = [[UIView alloc]initWithFrame:CGRectMake(x, y, widSize, heiSize)];
    [white setBackgroundColor:[UIColor whiteColor]];
    [whites addObject:white];
    [self.view addSubview:white];

of course they are in the screen and i can see it(i do it in loop) now, i put toch begin method and what it do it only tell me which uiview was pressed.

i want to do the same thing with touchedMoved and recognize if user drag is finger on the screen:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    NSArray* touchArray = [touches allObjects];
    for (int i = 0; i < [touchArray count]; ++i) {
        UITouch* touch = [touchArray objectAtIndex:i];
        UIView *key = [touch view];
        int i = [whiteKeys indexOfObject:key];
        if (i > 7) {
            int j = [whiteKeys indexOfObject:key];
            NSLog(@"%d",j);
            continue;
        }
        NSLog(@"%d",i);
    }
}

the problem is that when i moved my finger on the screen, he print me always the number of the view i pressed the screen in the start and not change it although i moved my finger on anoth开发者_如何学Cer uiview


This is a core concept of touch handling in iOS. The touches always belong to the view they started in - not the view that they move to during a touch. I would HIGHLY recommend watching the video on multi-touch from WWDC 2011 (one of the best of the conference) which goes into all of this in detail:

https://developer.apple.com/itunes/?destination=adc.apple.com.8270634034.08270634040.8367260921?i=1527940296


That's intended behavior. A UITouch's view is only set when it's created, so you'll only ever get the first view.

Here's some documentation of the UITouch's interface. http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITouch_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006785

view

@property(nonatomic, readonly, retain) UIView *view

The value of the property is the view object in which the touch originally occurred. This object might not be the view the touch is currently in.

Since you're storing your views in the array "whites," I guess you could iterate through them to see which view your touch is in.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜