adsense

2012年1月24日星期二

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




sliding



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
 }

@end


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 ");
        
    }
    
}

2011年6月28日星期二

check connection

之前有文章講用 NSURLCONNECTION 既 TIMEOUT 用來 CHECK 部 IPHONE 係未 CONNECTED

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

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

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

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

另外要加個 framework 叫 SystemConfiguration.framework


以下係 EXAMPLE

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

TestConnection.h

#import

@class Reachability;

@interface TestConnectionViewController : UIViewController {


Reachability* internetReach;


}

@end


TestConnection.m


- (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){
NSLog(@"connected");
}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){
NSLog(@"connected");
}else {
NSLog(@"not connected");
}


}

2011年6月8日星期三

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];
NSLog(@"%d",ans1);
}



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

2011年5月31日星期二

uiwebview 用法

此摘要無法顯示。請 按一下這裡以查看文章。

udid

上次講到 UDID , 

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


NSString *udid = [[UIDevice currentDevice] uniqueIdentifier];
NSLog(@"%@",udid);

2011年5月29日星期日





















如果想 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"
message:alertMessage
delegate:self cancelButtonTitle:@"Ignore"
otherButtonTitles:@"Buy Full Version",nil];
[alert show];
[alert release];
}

2008年2月11日星期一

做第一個 .com 網站

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

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

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


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

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

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

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

ServerName www.domain.com
serveralias domain.com *.domain.com
DocumentRoot e:/Apache2/htdocs/domain


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

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