加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift 图片下拉放大

发布时间:2020-12-14 07:04:40 所属栏目:百科 来源:网络整理
导读:let iconMargin: CGFloat = 10.0 let iconW: CGFloat = 80.0 let iconH: CGFloat = 80.0 let cellIdientifier = "cellIdentifier" let screenHeight = UIScreen .mainScreen () .bounds .size .height let screenWidth = UIScreen .mainScreen () .bounds .s
let iconMargin: CGFloat = 10.0
let iconW: CGFloat = 80.0
let iconH: CGFloat = 80.0
let cellIdientifier = "cellIdentifier"
let screenHeight = UIScreen.mainScreen().bounds.size.height
let screenWidth = UIScreen.mainScreen().bounds.size.width

在viewDidLoad中

// 这里创建tableview的代码就不贴出来了
        self.createSubviews() 
        // 调整tableview的位置
        self.tableView.contentInset = UIEdgeInsetsMake(200,0,0);
        // 把图片设置到对应位置上
        imageTopView = UIImageView.init(frame: CGRectMake(0,screenWidth,200))
        // 把加载的图片设置需要的大小
        let oldImage = UIImage.init(named: "1111")
        imageTopView.image = self.originImagescaleToSize(oldImage!,size: CGSizeMake(screenWidth,200))
        // 这里是为了让图片宽度随着高度的拉伸而拉伸
        imageTopView.contentMode = UIViewContentMode.ScaleAspectFill;
        self.view.addSubview(imageTopView)
        imageIcon = UIImageView.init(frame: CGRectMake(iconMargin,200 - (iconMargin + iconH),iconW,iconH))
        imageIcon.layer.cornerRadius = 7.5;
        imageIcon.image = UIImage.init(named: "01")
        imageIcon.clipsToBounds = true;
        //自动布局,自适应顶部
        imageIcon.autoresizingMask = UIViewAutoresizing.FlexibleTopMargin
        imageTopView.addSubview(imageIcon)

设置图片的大小

func originImagescaleToSize(image: UIImage,size: CGSize)->UIImage
    {
        UIGraphicsBeginImageContextWithOptions(size,false,0)
        image.drawInRect(CGRectMake(0,size.width,size.height))
        let scaleImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return scaleImage
    }

监听滚动

func scrollViewDidScroll(scrollView: UIScrollView) {
        // 偏移量y的变化
        let y: CGFloat = scrollView.contentOffset.y;
        print("y ------ (y)")
        if y < -200 {
            var frame: CGRect = self.imageTopView.frame;
            frame.size.height = -y;
            self.imageTopView.frame = frame;
        }
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读