xcode uiscrollview with paging and zoom

Here is a simple and sample code that demonstrate the photo slide function with zoom using uiscrollview.

this is the first photo


zooming in particular photo

1. first drag three photos to your project , size not important . 

2  viewcontroller.h 's code 

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIScrollViewDelegate>{
    UIScrollView *bgScorllView;        // background scrollview  control the slide paging
    UIView *bgView;                        // background view is on the bgscrollview
    UIImageView *picImageView;    //  the imageview for the photo where in the current screen
    UIScrollView *picScrollView;     // the scrollview for the current screen to preform zoom function
    UIImageView *preImageView;   // the imageview for the left side of the current screen

    UIImageView *nextImageView;  // the imageview for the right side of the current screen


3. code for viewcontroller.m

at viewdidload
1. define the contentsize of the view , it 's for sliding and paging on the bgscrollview
2. run loadPage

at loadPage

1. remove all subview at bgview first to save memory

2. load the current screen scroll view and imagview

3. add imageview at both sides of current screen , so even slide before reach that page , can see that images

4. add tag for the current screen scrollview , so it won't be confused for uiscrollview delegate - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

#import "ViewController.h"

@implementation ViewController

int numberOfPhotos = 3;

int currentpage;

- (void)didReceiveMemoryWarning
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.

-(void)loadPage :(int) page{
    currentpage = page;
    for (UIImageView *sView in bgView.subviews){
        NSLog(@"removesubview %@",sView);

         [sView removeFromSuperview];

    NSLog(@"page number %d",page);
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"iphonescroll%d.png",page]];
    picScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake((page-1)*320+10, 10,300,440)];
    picScrollView.delegate = self;
    picScrollView.maximumZoomScale =3;
    picScrollView.minimumZoomScale =1;
    picScrollView.zoomScale =1;
    picScrollView.clipsToBounds = YES;
    picScrollView.bounces = YES;
    picScrollView.scrollEnabled = YES;
    picScrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    picScrollView.tag = 5;
    picImageView = [[UIImageView alloc] initWithImage:image];
    [picImageView setFrame:CGRectMake(0, 0, 300,440)];
    [picScrollView addSubview:picImageView];
    [bgView addSubview:picScrollView];
    int nextpage = page +1 ;
    UIImage *nextimage = [UIImage imageNamed:[NSString stringWithFormat:@"iphonescroll%d.png",nextpage]];
    nextImageView = [[UIImageView alloc] initWithImage:nextimage];
    [nextImageView setFrame:CGRectMake((nextpage-1)*320+10, 10, 300,440)];
    [bgView addSubview:nextImageView];
    int prepage = page -1 ;
    UIImage *preimage = [UIImage imageNamed:[NSString stringWithFormat:@"iphonescroll%d.png",prepage]];
    preImageView = [[UIImageView alloc] initWithImage:preimage];
    [preImageView setFrame:CGRectMake((prepage-1)*320+10, 10, 300,440)];
    [bgView addSubview:preImageView];    


#pragma mark - View lifecycle

- (void)viewDidLoad
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    NSLog(@"%@",[UIScreen mainScreen]);
    bgScorllView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.view addSubview:bgScorllView];
    int width = 320*numberOfPhotos;
    bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width,460)];

    [self loadPage:1];
    [bgScorllView  setContentSize:CGSizeMake(width,460)];
    bgScorllView.pagingEnabled = YES;
    bgScorllView.delegate = self;
    bgScorllView.backgroundColor = [UIColor blueColor];
    [bgScorllView addSubview:bgView];
    //[self.view addSubview:bgView];

- (UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView
    //NSLog(@"viewforzooming scrollview tag %d",scrollView.tag);
    return picImageView;

- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    //NSLog(@"scrollviewdidzoom scrollview tag %d %f",scrollView.tag,scrollView.zoomScale);

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView.tag == 0){
        //NSLog(@"viewdisscroll length %f",scrollView.contentOffset.x);
        int pageNumber = floor(scrollView.contentOffset.x / 320 + 0.5) +1;
        //NSLog(@"page number %d",pageNumber);
        if (pageNumber != currentpage){
        [self loadPage:pageNumber];


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //NSLog(@"scrollviewdidscroll scrollview tag %d",scrollView.tag);
    if (scrollView.tag == 0){
        //NSLog(@"viewdisscroll length %f",scrollView.contentOffset.x);
        int pageNumber = floor(scrollView.contentOffset.x / 320 + 0.5) +1;
        //NSLog(@"page number %d",pageNumber);
        [self loadPage:pageNumber];

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    if (scrollView.tag == 0 ){
       // NSLog(@"viewdidendscroll ");


check connection


其實本身 APPLE 都出左個 CLASS,用來 CHECK
1. connected
2. wifi
3. mobile network connection

上網或係 XCODE 搵 reachability ,可以係 APPLE DOWNLOAD

個 PACKAGE 有 reachability.m , 同埋 reachability.h

要用個陣 抄入去係個 PROJECT 入边

另外要加個 framework 叫 SystemConfiguration.framework


我用黎係未 CONNECTED 可以參考 。。或者可以參考 reachability.m



@class Reachability;

@interface TestConnectionViewController : UIViewController {

Reachability* internetReach;




- (BOOL)checkInternetConnection{

BOOL connected;

internetReach = [[Reachability reachabilityForInternetConnection] retain];
[internetReach startNotifier];
NetworkStatus netStatus = [internetReach currentReachabilityStatus];

if (netStatus == NotReachable) {
NSLog(@"Internet Connection Fail");
connected = NO;
}else {
NSLog(@"Internet Connection Success");
connected = YES;
return connected;

//Called by Reachability whenever status changes.
- (void) reachabilityChanged: (NSNotification* )note
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
BOOL internetConnection = [self checkInternetConnection];

if (internetConnection){
}else {
NSLog(@"not connected");

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];

BOOL internetConnection = [self checkInternetConnection];

if (internetConnection){
}else {
NSLog(@"not connected");



method ? function?

我係 IPHONE 新手, 寫開 PHP , 開始見到 IPHONE D METHOD 真係睇到睇花 , 下边有個例子。。希望幫下好似我咁新手既人

-(int)add : (int)first addto :(int) second{
int ans;
ans = first + second;
return ans;

- (void)viewDidLoad {
    [super viewDidLoad];
int ans1;
int a = 10;
int b = 11
ans1 = [ self add:a addto:b];

好簡單, 就係將兩個數加埋, 


uiwebview 用法

uiwebview 好多人用, 因為 HTML 係大家常用, 習慣晒

今次會講 3 種 LOAD 資料入 UIWEBVIEW 既用法

個 UIWebView 叫 webView

第一種係 CALL bundle 既 HTML File

係 RESOURCES 內有 webview.html  同埋 images.jpeg

NSString *path = 
[[NSBundle mainBundle] pathForResource:@"webview" ofType:@"html"];
NSURL*url = [NSURL fileURLWithPath:path];

NSURLRequest *request=[NSURLRequest requestWithURL:url];
[webView loadRequest:request];


NSURL*url = [NSURL URLWithString:@""];

NSURLRequest *request=[NSURLRequest requestWithURL:url];
[webView loadRequest:request];


其實最簡單, 但係如果會用到 bundle 裡边既其他 FILE 就要留意

loadHTMLString:webstring baseURL:nil , 

如果無需要用到 BUNDLE 既 FILE 或者其他 URL 既 FILE , BaseURL:nil 就 OK 

但係今次例子會用到 BUNDLE 上边既 FILE 

NSString *webstring =@"<h1>testing</h1>hello hello<img src=images.jpeg>";

NSString *imagePath=[[NSBundle mainBundle] resourcePath];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

[webView loadHTMLString:webstring baseURL:
[NSURL URLWithString:[NSString stringWithFormat:@"file:/%@//",imagePath]]];


上次講到 UDID , 

下边就係獲取機中 UDID , 呢個 ID 唔同, 所以當有 ACTION 行, 你可以經 WEB PASS 個 UDID 去你自已個 SERVER , 咁就可以數住個 USER 用左幾多次, 就算係洗左個 PROGRAM 再裝番都無用

NSString *udid = [[UIDevice currentDevice] uniqueIdentifier];


如果想 USER 用特定次數便提示用家比 “錢” 買 完整版 , 可以用呢個方法

本身 APPS 有個叫nsuserdefault 可以儲存資料, 例如APPS 登入資料, 用過幾次等

今次呢個 APPS 如果用家用過一次便會收 nsuserdefault 既 counter +1 , 除左儲 integer , 可以儲 string , 什至 array 都得

係你個 ACTION 個度加入。

int calCounter = [[NSUserDefaults standardUserDefaults] integerForKey:@"counter"];
calCounter +=1;
[[NSUserDefaults standardUserDefaults] setInteger:calCounter forKey:@"counter"];
[[NSUserDefaults standardUserDefaults] synchronize];
calCounter = [[NSUserDefaults standardUserDefaults] integerForKey:@"counter"];
NSLog(@"new counter %d",calCounter);

最後個兩句係比 CONSOLE 睇結果, 加唔加無乜所謂

係每次行 ACTION 前后


calcounter%10 == 0 , 即係每 10 次問一問 , 然后加入一個 FUNCTION

calCounter = [[NSUserDefaults standardUserDefaults] integerForKey:@"counter"];
if ( calCounter%10 == 0 && calCounter >1){
NSString *alertMessage = [NSString stringWithFormat:@"%d times calculation has been made",calCounter];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Go Get Full Version"
delegate:self cancelButtonTitle:@"Ignore"
otherButtonTitles:@"Buy Full Version",nil];
[alert show];
[alert release];


做第一個 .com 網站

因為本身係屋企, 唔同 datacenter 有 nameserver, 所以要借助第三者幫手 , 叫做 DNS redirect
出面有好多類似既服務 , 例如, 或者 , 香港都有類似既服務, 每年 100 蚊, 不個 zoneedit 每個 account 有 5 個 free, 服務亦穏定

1.首先你要 check 下你 domain 仲有冇 , 可以去 睇下

2.如果有 , 可以去 zoneedit 開一個 account , 佢地會比一個 name server address 例如

3.然後你要去 register 一個 account, 可以有好多地方, 例如 , 每個 domain 都係 us8, 至於要香港 domain , 就一定要去,

申請 domain , 係 nameserver 入番頭先比你個個 nameserver

4.然後你去番 zoneedit , 將個 website 指向番自己部電腦, 入番個 ip 你可以有唔同既 subdomain , 指向唔同 ip 既電腦如果簡單 d , 你可以入 * 即係無論個 subdomain 入乜都係去番同一個 ip 既電腦.

5 跟住去番自己個 apache folder , 開番 conf folder 裡面既 httpd.conf

serveralias *
DocumentRoot e:/Apache2/htdocs/domain

6. 跟住你去番 htdocs 開番個 domain 個 folder , 放番你 d html file , 咁就成功

7 你可以試打 就攪掂, 不過提提你, 新申請既 domain 要 幾個鐘至 1 日至可以生效