iOS 提示文字 类似微信拍摄小视频“手指不要放开”


- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    NSLog(@"touchesEnded");

    

    UITouch *touch = [touches anyObject];

    CGPoint point = [touch locationInView:self.view];

    BOOL condition = [self isInBtnRect:point];

    /*

     结束时候咱们设定有两种情况依然算录制成功

     1.抬手时,录制时长 > 1/3总时长

     2.录制进度条完成时,就算手指超出按钮范围也算录制成功 -- 此时 end 方法不会调用,因为用户手指还在屏幕上,所以直接代码调用录制成功的方法,将控制器切换

     */

    

    if (condition) {

        NSLog(@"手指还在按钮范围之内");

        

        if (self.progressWidth.constant < SCREEN_WIDTH * 0.67) {

            //录制完成

            [self recordComplete];

        }else{

//            showAtTop(@"手指不要放开");

//            [ToastUtils showAtTop:[NSString stringWithFormat:@"ToastUtils"]];

            [ToastUtils showToast:[NSString stringWithFormat:@"手指不要放开"] atFrameY:SCREEN_HEIGHT - 130];

        }

    }

    

    [self stopAnimation];

    self.changeBtn.hidden = self.flashModelBtn.hidden = NO;

}


+ (void)showToast:(NSString *)message atFrameY:(CGFloat)frameY{


    [self show:message atFrameY:frameY showTime:2.0];

    

}


static UILabel *toastView = nil;


+ (void)show:(NSString *)message atFrameY:(CGFloat)frameY showTime:(float)showTime{

    if (![[NSThread currentThread] isMainThread]) {

        dispatch_async(dispatch_get_main_queue(), ^{

            [self show:message atFrameY:frameY showTime:showTime];

        });

        return;

    }

    @synchronized(self){

        if (toastView == nil) {

            toastView = [[UILabel alloc] init];

            toastView.backgroundColor = REDCOLOR;

            toastView.textColor = [UIColor whiteColor];

            toastView.font = [UIFont systemFontOfSize:17];

            toastView.layer.masksToBounds = YES;

            toastView.layer.cornerRadius = 3.0f;

            toastView.textAlignment = NSTextAlignmentCenter;

            toastView.alpha = 0;

            toastView.numberOfLines = 0;

            toastView.lineBreakMode = NSLineBreakByCharWrapping;

            [[UIApplication sharedApplication].keyWindow addSubview:toastView];

        }

    }

    if (toastView.superview != [UIApplication sharedApplication].keyWindow) {

        [toastView removeFromSuperview];

        [[UIApplication sharedApplication].keyWindow addSubview:toastView];

    }

    

    //    CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;

    

    CGFloat width = [self stringText:message font:18 isHeightFixed:YES fixedValue:30];

    CGFloat height = 30;

    if (width > screenWidth - 20) {

        width = screenWidth - 20;

        height = [self stringText:message font:18 isHeightFixed:NO fixedValue:width];

    }

    

    CGRect frame = CGRectMake(([UIScreen mainScreen].bounds.size.width-width)/2,frameY, width, height);

    toastView.alpha = 1;

    toastView.text = message;

    toastView.frame = frame;

    [UIView animateWithDuration:showTime animations:^{

        toastView.alpha = 0;

    } completion:^(BOOL finished) {

    }];


}



//根据字符串长度获取对应的宽度或者高度

+ (CGFloat)stringText:(NSString *)text font:(CGFloat)font isHeightFixed:(BOOL)isHeightFixed fixedValue:(CGFloat)fixedValue

{

    CGSize size;

    if (isHeightFixed) {

        size = CGSizeMake(MAXFLOAT, fixedValue);

    } else {

        size = CGSizeMake(fixedValue, MAXFLOAT);

    }

    

    CGSize resultSize;

    //返回计算出的size

    resultSize = [text boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:font]} context:nil].size;

    

    if (isHeightFixed) {

        return resultSize.width;

    } else {

        return resultSize.height;

    }

}





猜你喜欢

转载自blog.csdn.net/qq_27247497/article/details/52596900