ios


How to make the pageControl swipe effect? The scroll view slides continuously so far


I would like to make the swipe effect with the scrollView, I found a project where nothing is specifically invoked in the code or in the storyboard. I repeated the code, but in my project, the scrollView just slides continuously, it does not swipe from one imageView to the other. Would you know how to do this?
Here is the code, the scroll respond to the touch, the pagecontrol is a simple iboutlet, but there is no swipe effect (the scroll stops where the touch ends, instead of going back or moving the entire imageview ) :
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var pageControl: UIPageControl!
#IBOutlet weak var scrollView: UIScrollView!
var pageViews: [UIImageView?] = []
var pageImages : [UIImage?] = []
override func viewDidLoad() {
super.viewDidLoad()
pageImages = [UIImage(named:"photo1.png"),
UIImage(named:"photo2.png"),
UIImage(named:"photo3.png"),
UIImage(named:"photo4.png")]
let pageCount = pageImages.count
pageControl.currentPage = 0
pageControl.numberOfPages = pageCount
for _ in 0..<pageCount {
pageViews.append(nil)
}
let pagesScrollViewSize = scrollView.frame.size
println("pagesScrollViewSize : \(pagesScrollViewSize)")
scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * CGFloat(pageCount), pagesScrollViewSize.height)
loadVisiblePages()
}
//when scrolling
func scrollViewDidScroll(scrollView: UIScrollView!){
loadVisiblePages()
}
func loadVisiblePages(){
let pageWidth = scrollView.frame.size.width
let page = Int(floor( (scrollView.contentOffset.x * 2.0 + pageWidth) / (pageWidth*2.0) ))
println("page: \(page)")
pageControl.currentPage = page
let firstPage = page-1
let lastPage = page+1
//remove all the pages before firstPage
for var index = 0; index < firstPage; ++index{
purgePage(index)
}
//load pages in our range
for var index = firstPage; index <= lastPage; ++index {
loadPage(index)
}
//remove after lastPage
for var index = lastPage+1; index < pageImages.count; ++index {
purgePage(index)
}
}
func loadPage(index:Int){
if index < 0 || index >= pageImages.count {
return
}
if let pageView = pageViews[index] {
//already loaded
}
else {
var frame = scrollView.bounds
frame.origin.x = frame.size.width * CGFloat(index)
frame.origin.y = 0.0
println("\(frame)")
var newImageView = UIImageView(image:pageImages[index])
newImageView.contentMode = .ScaleAspectFit
newImageView.frame = frame
scrollView.addSubview(newImageView)
pageViews[index] = newImageView
}
}
func purgePage(index:Int){
if index < 0 || index >= pageImages.count {
return
}
if let pageView = pageViews[index] {
pageView.removeFromSuperview()
pageViews[index] = nil
}
}
}
UIPageControl doesn't do the page-scrolling behavior — it provides only the dots that indicate which page is shown (and that a user can tap to jump between pages). Use the pagingEnabled property of UIScrollView to scroll by page instead of continuously.
Or just use UIPageViewController to get just about all the scrolling and paging business handled for you.

Related Links

Call pushViewController after presentViewController in Swift
Swift 3 no subscript error
Two segues passing the same data from one view controller
SVG transform=“rotate(x,x,x)” not working on iOS
Concatenating values to the same key in an NSMutableDictionary
React native app is not working with Xcode 8
AVFoundation just Photo option-> no microphone accesss request (no video)
How to get which view in the hierarchy was touched?
Debugging URLSession (in iOS 10)
How upload video into server in Objective C ios
swift asynchronous request using dispatch_group_notify doesn't work
Xcode Undefined symbols for architecture x86_64:
How to find the average pixel colour of a defined area in UI image
How to loop in each dictionary key and value and print in UILabel
TableViewController Detect Row Tap with Static Cells
Navigation bar shows white space

Categories

HOME
html5-canvas
elf
graylog2
windows-runtime
libreoffice
httpbuilder-ng
range
gcov
development-environment
w3.css
frequency
command-line-interface
zend-expressive
openui5
google-cloud-sdk
avr
cloudrail
export-to-excel
round-robin
silk-performer
rtp
nintex-workflow
django-autocomplete-light
dsc
mouseevent
ssms-2014
sax
italic
angular-services
spring-batch-admin
masstransit
android-imageview
panel-data
pyparsing
signing
bem
sagepay
project-centennial
jquery-ui-dialog
sdcc
kendo-chart
pseudo-element
ewsjavaapi
scoring
xbrl
orchestrate
database-cleaner
paypal-webhooks
itertools
getelementbyid
replicate
jeditorpane
itamae
mysql-notifier
logstash-jdbc
perspective
ckcontainer
agents-jade
texture2d
pygments
target-platform
ota
blpapi
id3
python-dragonfly
delphi-10-seattle
many-to-one
rdotnet
core-data-migration
simperium
gevent-socketio
mks
android-design-library
simpleadapter
prettyfaces
django-postgresql
catalina.out
subdirectory
dynamic-typing
nhunspell
dotliquid
modx-evolution
couchrest
nsusernotificationcenter
zephir
mainwindow
globbing
python-dateutil
documentviewer
code-structure
vim-fugitive
ria
media-manager
ruby-1.9.2
gemstone
sharepoint-timer-job
nsindexset
jboss-cache
excel-web-query
scramble
webshop
gendarme
web-based
variable-names

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App