SoFunction
Updated on 2025-04-13

iOS realizes application floating window effect

This article shares the specific code for iOS to implement the effect of the floating window for your reference. The specific content is as follows

need

Add a floating window at the top of an app, just like the AssistiveTouch in the iOS system can slide left and right, but it will eventually stop on the left or right.

Implementation ideas

Add a UIWindow to the top of the application view, use this UIWindow as a floating window, add moving gesture monitoring to the UIWindow, let the floating window move with your fingers, and when released, let it move in an animation way.

Code

//Suspended window test//Create a floating windowmwindow = [[AssistiveTouch alloc]initWithFrame:CGRectMake(100, 200, 40, 40) imageName:@""];
//The rootview must be set in the ios9 window or it will crashUIViewController *controller = [[UIViewController alloc] init];
 = controller;
//Display the floating window.  .[ makeKeyAndVisible];
//Add a move gestureUIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(locationChange:)];
  = YES;
 [self addGestureRecognizer:pan];
//Change position-(void)locationChange:(UIPanGestureRecognizer*)p
{
    //[[UIApplication sharedApplication] keyWindow]
    CGPoint panPoint = [p locationInView:[[UIApplication sharedApplication] keyWindow]];
    if( == UIGestureRecognizerStateBegan)
    {
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(changeColor) object:nil];
        _imageView.alpha = 0.8;
    }
    else if ( == UIGestureRecognizerStateEnded)
    {
        [self performSelector:@selector(changeColor) withObject:nil afterDelay:4.0];
    }
    if( == UIGestureRecognizerStateChanged)
    {
         = CGPointMake(, );
    }
    else if( == UIGestureRecognizerStateEnded)
    {
        if( <= kScreenWidth/2)
        {
            if( <= 40+HEIGHT/2 &&  >= 20+WIDTH/2)
            {
                [UIView animateWithDuration:0.2 animations:^{
                     = CGPointMake(, HEIGHT/2);
                }];
            }
            else if( >= kScreenHeight-HEIGHT/2-40 &&  >= 20+WIDTH/2)
            {
                [UIView animateWithDuration:0.2 animations:^{
                     = CGPointMake(, kScreenHeight-HEIGHT/2);
                }];
            }
            else if ( < WIDTH/2+15 &&  > kScreenHeight-HEIGHT/2)
            {
                [UIView animateWithDuration:0.2 animations:^{
                     = CGPointMake(WIDTH/2, kScreenHeight-HEIGHT/2);
                }];
            }
            else
            {
                CGFloat pointy =  < HEIGHT/2 ? HEIGHT/2 :;
                [UIView animateWithDuration:0.2 animations:^{
                     = CGPointMake(WIDTH/2, pointy);
                }];
            }
        }
        else if( > kScreenWidth/2)
        {
            if( <= 40+HEIGHT/2 &&  < kScreenWidth-WIDTH/2-20 )
            {
                [UIView animateWithDuration:0.2 animations:^{
                     = CGPointMake(, HEIGHT/2);
                }];
            }
            else if( >= kScreenHeight-40-HEIGHT/2 &&  < kScreenWidth-WIDTH/2-20)
            {
                [UIView animateWithDuration:0.2 animations:^{
                     = CGPointMake(, 480-HEIGHT/2);
                }];
            }
            else if ( > kScreenWidth-WIDTH/2-15 &&  < HEIGHT/2)
            {
                [UIView animateWithDuration:0.2 animations:^{
                     = CGPointMake(kScreenWidth-WIDTH/2, HEIGHT/2);
                }];
            }
            else
            {
                CGFloat pointy =  > kScreenHeight-HEIGHT/2 ? kScreenHeight-HEIGHT/2 :;
                [UIView animateWithDuration:0.2 animations:^{
                     = CGPointMake(320-WIDTH/2, pointy);
                }];
            }
        }
    }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.