Swift 图片下拉放大

前端之家收集整理的这篇文章主要介绍了Swift 图片下拉放大前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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;
        }
    }
原文链接:https://www.f2er.com/swift/324044.html

猜你在找的Swift相关文章