触摸移动离开框架

发布于 2024-10-04 11:11:14 字数 2194 浏览 5 评论 0原文

我试图弄清楚如何将按钮设置为在 TouchMoved 离开当前所选按钮的框架时不突出显示。请参阅下面的代码。下面的代码应该允许单独按下和滑动三个按钮中的任何一个。我遇到的问题是,当按钮滑动时,例如使用touchesmoved,按钮会调用操作,但永远不会将按钮设置回在滑动离开按钮框架时不突出显示。

预先感谢,

阿扎

- (void)touchesMoved: (NSSet *)touches withEvent:(UIEvent *)event {
for(UITouch *t in touches) { 

CGPoint location = [t locationInView:t.view]; 

if((CGRectContainsPoint(Button1.frame, location))  && (!Button1.isHighlighted))
{ 
    if (!Button1.isHighlighted){ 
       [Button1 setHighlighted:YES];
       [self doAction1]; 
     }else{
       [Button1 setHighlight:NO];
     }
 }

if((CGRectContainsPoint(Button2.frame, location))  && (!Button2.isHighlighted))
{ 
    if (!Button2.isHighlighted){ 
       [Button2 setHighlighted:YES];
       [self doAction2]; 
     }else{
       [Button2 setHighlight:NO];
     }
 }

if((CGRectContainsPoint(Button3.frame, location))  && (!Button3.isHighlighted))
{ 
    if (!Button3.isHighlighted){ 
       [Button3 setHighlighted:YES];
       [self doAction3]; 
     }else{
       [Button3 setHighlight:NO];
     }
 }
 }

- (void)touchesBegan: (NSSet *)touches withEvent:(UIEvent *)event {
for(UITouch *t in touches) { 

CGPoint location = [t locationInView:t.view]; 

if(CGRectContainsPoint(Button1.frame, location))
{ 
    if (!Button1.isHighlighted){ 
       [Button1 setHighlighted:YES];
       [self doAction1]; 
     }
 }

if(CGRectContainsPoint(Button2.frame, location)) 
{ 
    if (!Button2.isHighlighted){ 
       [Button2 setHighlighted:YES];
       [self doAction2]; 
     }
 }

if(CGRectContainsPoint(Button3.frame, location))
{ 
    if (!Button3.isHighlighted){ 
       [Button3 setHighlighted:YES];
       [self doAction3]; 
     }
 }
 }


- (void)touchesEnded: (NSSet *)touches withEvent:(UIEvent *)event {

    for (UITouch *t in touches){
    CGPoint location = [t locationInView:self.view]; 

    if(CGRectContainsPoint(Button1.frame, location)) {    
        [Button1 setHighlighted:NO];

    } else if(CGRectContainsPoint(Button2.frame, location)) { 
           [Button2 setHighlighted:NO];

    } else if(CGRectContainsPoint(Button3.frame, location)) { 
       [Button3 setHighlighted:NO];
    }
   }
 }

I am trying to figure out how I can set a button to not be highlighted when touchesmoved leaves the frame of the current selected button. See code below. The below code should allow any of the three buttons to be pressed individually and swipped. The problem I am having is that when the buttons a swipped eg using touchesmoved the buttons call the Actions but never set the buttons back to not be highlighted when the swipe leaves the button frame.

Thanks in advance,

Azza

- (void)touchesMoved: (NSSet *)touches withEvent:(UIEvent *)event {
for(UITouch *t in touches) { 

CGPoint location = [t locationInView:t.view]; 

if((CGRectContainsPoint(Button1.frame, location))  && (!Button1.isHighlighted))
{ 
    if (!Button1.isHighlighted){ 
       [Button1 setHighlighted:YES];
       [self doAction1]; 
     }else{
       [Button1 setHighlight:NO];
     }
 }

if((CGRectContainsPoint(Button2.frame, location))  && (!Button2.isHighlighted))
{ 
    if (!Button2.isHighlighted){ 
       [Button2 setHighlighted:YES];
       [self doAction2]; 
     }else{
       [Button2 setHighlight:NO];
     }
 }

if((CGRectContainsPoint(Button3.frame, location))  && (!Button3.isHighlighted))
{ 
    if (!Button3.isHighlighted){ 
       [Button3 setHighlighted:YES];
       [self doAction3]; 
     }else{
       [Button3 setHighlight:NO];
     }
 }
 }

- (void)touchesBegan: (NSSet *)touches withEvent:(UIEvent *)event {
for(UITouch *t in touches) { 

CGPoint location = [t locationInView:t.view]; 

if(CGRectContainsPoint(Button1.frame, location))
{ 
    if (!Button1.isHighlighted){ 
       [Button1 setHighlighted:YES];
       [self doAction1]; 
     }
 }

if(CGRectContainsPoint(Button2.frame, location)) 
{ 
    if (!Button2.isHighlighted){ 
       [Button2 setHighlighted:YES];
       [self doAction2]; 
     }
 }

if(CGRectContainsPoint(Button3.frame, location))
{ 
    if (!Button3.isHighlighted){ 
       [Button3 setHighlighted:YES];
       [self doAction3]; 
     }
 }
 }


- (void)touchesEnded: (NSSet *)touches withEvent:(UIEvent *)event {

    for (UITouch *t in touches){
    CGPoint location = [t locationInView:self.view]; 

    if(CGRectContainsPoint(Button1.frame, location)) {    
        [Button1 setHighlighted:NO];

    } else if(CGRectContainsPoint(Button2.frame, location)) { 
           [Button2 setHighlighted:NO];

    } else if(CGRectContainsPoint(Button3.frame, location)) { 
       [Button3 setHighlighted:NO];
    }
   }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逐鹿 2024-10-11 11:11:14

好的,我知道我以前处理过同样的问题。我还在代码中我自己的注释中看到了对它的引用。我附加了一大块非常旧的代码,我编写这些代码来处理模拟按钮(类似于您正在做的事情)。也许浏览一下这个会有所帮助:

typedef struct {
            BOOL enabled;
            BOOL isActivated;
            NSTimeInterval activatedStartTime;
            NSTimeInterval holdTime;
            CGRect frame;
            UIImageView *glowImage;
            BOOL processPending;
            SEL callWhenTouched;
            } SIM_BUTTON;



- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    int fingers = [touches count];
    UITouch *touch;
    CGPoint touchPoint;
    int i, j;

    for (i=0; i<fingers; i++)
        {
        touch = [[touches allObjects] objectAtIndex:i];
        touchPoint = [touch locationInView:self.view];


        for ( j=0; j<SIM_BUTTON_COUNT; j++)
            {
            if ( simButton[j].enabled && CGRectContainsPoint( simButton[j].frame, touchPoint) )
                {
                simButton[j].isActivated=YES;
                simButton[j].activatedStartTime = [NSDate timeIntervalSinceReferenceDate];
                simButton[j].processPending = YES;
                }
            }
        }
    }



- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
    int fingers = [touches count];
    UITouch *touch;
    CGPoint touchPoint;
    CGPoint previousTouchPoint;
    int i, j;

    for (i=0; i<fingers; i++)
        {
        touch = [[touches allObjects] objectAtIndex:i];
        touchPoint = [touch locationInView:self.view];
        previousTouchPoint = [touch previousLocationInView:self.view];

        for ( j=0; j<SIM_BUTTON_COUNT; j++)
            {
            if ( simButton[j].enabled && simButton[j].isActivated && CGRectContainsPoint( simButton[j].frame, touchPoint) && CGRectContainsPoint( simButton[j].frame, previousTouchPoint) )
                {
                simButton[j].isActivated=YES;
                }
            else
            if ( simButton[j].enabled && !simButton[j].isActivated && CGRectContainsPoint( simButton[j].frame, touchPoint) && CGRectContainsPoint( simButton[j].frame, previousTouchPoint) )
                {
                simButton[j].activatedStartTime = [NSDate timeIntervalSinceReferenceDate];
                simButton[j].isActivated=YES;
                }
            else
            if ( simButton[j].enabled && simButton[j].isActivated && !CGRectContainsPoint( simButton[j].frame, touchPoint) && CGRectContainsPoint( simButton[j].frame, previousTouchPoint) )
                {
                simButton[j].isActivated=NO;
                simButton[j].activatedStartTime = (NSTimeInterval)0.0;
                }
            else
            if ( simButton[j].enabled && !simButton[j].isActivated && CGRectContainsPoint( simButton[j].frame, touchPoint) && !CGRectContainsPoint( simButton[j].frame, previousTouchPoint) )
                {
                simButton[j].isActivated=YES;
                simButton[j].activatedStartTime = [NSDate timeIntervalSinceReferenceDate];
                simButton[j].processPending = YES;
                }

            //
            // If the user touched the SCAN button and then slid their finger off of the SCAN button
            //
            if ( scannerActive==YES && simButton[RIGHT_SCAN_BUTTON].isActivated==NO && CGRectContainsPoint( simButton[RIGHT_SCAN_BUTTON].frame, previousTouchPoint) )
                {
                phraseMode = EXTERNAL_SCAN_PHRASES;     // Default
                }
            }
        }
    }



- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
    int fingers = [touches count];
    UITouch *touch;
    CGPoint touchPoint;
    CGPoint previousTouchPoint;
    int i, j;

    for (i=0; i<fingers; i++)
        {
        touch = [[touches allObjects] objectAtIndex:i];
        touchPoint = [touch locationInView:self.view];
        previousTouchPoint = [touch previousLocationInView:self.view];

        for ( j=0; j<SIM_BUTTON_COUNT; j++)
            {
            if ( simButton[j].enabled && simButton[j].isActivated && (CGRectContainsPoint( simButton[j].frame, touchPoint) || CGRectContainsPoint( simButton[j].frame, previousTouchPoint)) )
                {
                simButton[j].isActivated=NO;
                simButton[j].activatedStartTime = (NSTimeInterval)0.0;
                }
            }
        }
    }



- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    {
    [self touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event];
    }

OK, I know I've handled this same issue before. I also see a reference to it in my own comments in the code. I've attached a chunk of very old code that I wrote to handle simulated buttons (similar to what you are doing). Maybe looking through this will help:

typedef struct {
            BOOL enabled;
            BOOL isActivated;
            NSTimeInterval activatedStartTime;
            NSTimeInterval holdTime;
            CGRect frame;
            UIImageView *glowImage;
            BOOL processPending;
            SEL callWhenTouched;
            } SIM_BUTTON;



- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    int fingers = [touches count];
    UITouch *touch;
    CGPoint touchPoint;
    int i, j;

    for (i=0; i<fingers; i++)
        {
        touch = [[touches allObjects] objectAtIndex:i];
        touchPoint = [touch locationInView:self.view];


        for ( j=0; j<SIM_BUTTON_COUNT; j++)
            {
            if ( simButton[j].enabled && CGRectContainsPoint( simButton[j].frame, touchPoint) )
                {
                simButton[j].isActivated=YES;
                simButton[j].activatedStartTime = [NSDate timeIntervalSinceReferenceDate];
                simButton[j].processPending = YES;
                }
            }
        }
    }



- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
    int fingers = [touches count];
    UITouch *touch;
    CGPoint touchPoint;
    CGPoint previousTouchPoint;
    int i, j;

    for (i=0; i<fingers; i++)
        {
        touch = [[touches allObjects] objectAtIndex:i];
        touchPoint = [touch locationInView:self.view];
        previousTouchPoint = [touch previousLocationInView:self.view];

        for ( j=0; j<SIM_BUTTON_COUNT; j++)
            {
            if ( simButton[j].enabled && simButton[j].isActivated && CGRectContainsPoint( simButton[j].frame, touchPoint) && CGRectContainsPoint( simButton[j].frame, previousTouchPoint) )
                {
                simButton[j].isActivated=YES;
                }
            else
            if ( simButton[j].enabled && !simButton[j].isActivated && CGRectContainsPoint( simButton[j].frame, touchPoint) && CGRectContainsPoint( simButton[j].frame, previousTouchPoint) )
                {
                simButton[j].activatedStartTime = [NSDate timeIntervalSinceReferenceDate];
                simButton[j].isActivated=YES;
                }
            else
            if ( simButton[j].enabled && simButton[j].isActivated && !CGRectContainsPoint( simButton[j].frame, touchPoint) && CGRectContainsPoint( simButton[j].frame, previousTouchPoint) )
                {
                simButton[j].isActivated=NO;
                simButton[j].activatedStartTime = (NSTimeInterval)0.0;
                }
            else
            if ( simButton[j].enabled && !simButton[j].isActivated && CGRectContainsPoint( simButton[j].frame, touchPoint) && !CGRectContainsPoint( simButton[j].frame, previousTouchPoint) )
                {
                simButton[j].isActivated=YES;
                simButton[j].activatedStartTime = [NSDate timeIntervalSinceReferenceDate];
                simButton[j].processPending = YES;
                }

            //
            // If the user touched the SCAN button and then slid their finger off of the SCAN button
            //
            if ( scannerActive==YES && simButton[RIGHT_SCAN_BUTTON].isActivated==NO && CGRectContainsPoint( simButton[RIGHT_SCAN_BUTTON].frame, previousTouchPoint) )
                {
                phraseMode = EXTERNAL_SCAN_PHRASES;     // Default
                }
            }
        }
    }



- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
    int fingers = [touches count];
    UITouch *touch;
    CGPoint touchPoint;
    CGPoint previousTouchPoint;
    int i, j;

    for (i=0; i<fingers; i++)
        {
        touch = [[touches allObjects] objectAtIndex:i];
        touchPoint = [touch locationInView:self.view];
        previousTouchPoint = [touch previousLocationInView:self.view];

        for ( j=0; j<SIM_BUTTON_COUNT; j++)
            {
            if ( simButton[j].enabled && simButton[j].isActivated && (CGRectContainsPoint( simButton[j].frame, touchPoint) || CGRectContainsPoint( simButton[j].frame, previousTouchPoint)) )
                {
                simButton[j].isActivated=NO;
                simButton[j].activatedStartTime = (NSTimeInterval)0.0;
                }
            }
        }
    }



- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    {
    [self touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event];
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文