Visual difference effect of mg--scrolling

Source: Internet
Author: User

#几句代码完成tableView滚动的视觉差

-(Loss of frame severity)
! [] (Http://upload-images.jianshu.io/upload_images/1429890-f2c857700f043939.gif?imageMogr2/auto-orient/strip)


-# # #补录一张好一点的
! [. gif] (Http://upload-images.jianshu.io/upload_images/1429890-160c57b488c1c694.gif?imageMogr2/auto-orient/strip)


>-# # # #主要原理:
-##### #1, set UI. To put it simply, add a uiview to Tableviewcell first, The size of this view is equal to cell or equal to Cell.contentview, and then add a uiimageview to this view, the constraint of this imageview is left and right to 0, horizontally centered, and then set it to a height greater than the cell (the exact number can be based on the actual large minor of the picture Full) because the size of the parent control is not displayed, this is set to 400.
-##### #2, is the proxy method in Uitableviewdelegate ' Func scrollviewdidscroll (_ Scrollview:uiscrollview) ' The IMAGEVIEW.ORIGIN.Y value is dynamically modified in the method, resulting in an illusion that the image is scrolled visually. Second, it is to be added that we are dealing with cells that appear on the screen, not all cells, So we get all the cell,for loops that appear on the screen through the Self.tableView.visibleCells to imageview the parent view through the cell, FRAME.ORIGIN.Y by its judgment, and then assign the value to ImageView . FRAME.ORIGIN.Y for final results

***
-* * Description * *: This is done in storyboard, so in SB to view and ImageView binding a tag, in order to take the two controls, of course, you can also customize the cell, and then in the custom cell out of line, you do not need to bind tag, These two controls can be removed through the properties, which makes it easier to
- ! [] (http://upload-images.jianshu.io/upload_images/1429890-0ef9eecbc92ec46d.png?imageMogr2/auto-orient/strip% 7cimageview2/2/w/1240)
- ! [Auxiliary diagram] (http://upload-images.jianshu.io/upload_images/1429890-e96b574a45622974.png?imageMogr2/auto-orient/strip% 7cimageview2/2/w/1240)
***

-# # #代码区域

```
Viewcontroller.swift
Mgtableview
Import UIKit
Class Viewcontroller:uiviewcontroller {
@IBOutlet weak var tableview:uitableview!
Data source
Fileprivate lazy var Dataarr: [UIImage] = {
var arr: [UIImage] = [UIImage] ()
For _ in 1...10 {
For I in 1...10 {
Let image = UIImage (named:string (format: "%02d", I))
Arr.append (image!)
}
}
Return arr
}()
Override Func Viewdidload () {
Super.viewdidload ()
Tableview.layoutmargins = Uiedgeinsets.zero
Tableview.separatorinset = Uiedgeinsets.zero
Tableview.separatorstyle =. None
Tableview.rowheight = 300
}
}

//MARK:-uitableviewdatasource
Extension viewcontroller:uitableviewdatasource {
Func TableView (_ TableView : UITableView, Numberofrowsinsection section:int), Int {
return dataarr.count
}

Func tableView (_ Ta Bleview:uitableview, Cellforrowat Indexpath:indexpath), UITableViewCell {
Let cell = tableview.dequeuereusable Cell (withidentifier: "Cellid", For:indexpath)
Let Imagev = Cell.viewwithtag (+) as! Uiimageview
Imagev.image = dataarr[indexpath.row]
return cell
}
}

MARK:-Uitableviewdelegate
Extension viewcontroller:uitableviewdelegate{
Func Scrollviewdidscroll (_ Scrollview:uiscrollview) {
For cell in Self.tableView.visibleCells {
Remove the parent view of ImageView and ImageView
Let Imagevparentv = Cell.viewwithtag (1001)
Let Imagev = Cell.viewwithtag (1000)
Let rect = Imagevparentv?. Convert (Imagevparentv?. Bounds)!, To:nil)
var y = UIScreen.main.bounds.size.height-(rect?. ORIGIN.Y)! -560
var y =-(rect?. ORIGIN.Y)!
Y *= 0.2
Determine the FRAME.ORIGIN.Y of ImageView's parent view
If y>0 {
Y=0
}
If y <-200 {
y =-200
}
Re-assign the value IMAGEVIEW.FRAME.ORIGIN.Y
Imagev?. FRAME.ORIGIN.Y = y
}
}
}```

Visual difference effect of mg--scrolling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.