开发者

Shake iphone to view random html files

开发者 https://www.devze.com 2022-12-10 16:26 出处:网络
i have 20 HTML files ok ? i am going toshake the iphone , after shook , one of 20 html files shows up as random .

i have 20 HTML files ok ?

i am going to shake the iphone , after shook , one of 20 html files shows up as random . i don't know random value on the objective C . can u help me ? here is my code :

#pragma mark -
- (void)accelerometer:(UIAccelerom开发者_如何学Pythoneter *)accelerometer 
        didAccelerate:(UIAcceleration *)acceleration {
    {
        if (acceleration.x > kAccelerationThreshold 
            || acceleration.y > kAccelerationThreshold
            || acceleration.z > kAccelerationThreshold) {
            // image hidden 
            shakeIcon.hidden = YES;

            //Random HTML view But here show only one . 
            NSString *path = [[NSBundle mainBundle] pathForResource:@"webViewContent" ofType:@"html"];
            NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];

            NSString *htmlString = [[NSString alloc] initWithData: [readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];

            [self.falView loadHTMLString:htmlString baseURL:nil];
            [htmlString release];


        }
    }
}

for example my html file names are : Myweb 1 ,Myweb 2 , 3,4,5,6,7,8,9 ............


You can get a random number by using rand(). Take a look at generating random numbers in objective c


I picked this code up from somewhere in SO or on the net, sorry to original author that I can't attribute source correctly. Didn't expect I'd be reposting it again. I didn't test it extensively to ensure it will in fact generate

#define MYRAND(from, to) ((int)from + arc4random() % (to-from+1))
...

EDIT //

if (acceleration.x > kAccelerationThreshold 
    || acceleration.y > kAccelerationThreshold
    || acceleration.z > kAccelerationThreshold) {
    // image hidden 
        shakeIcon.hidden = YES;

        //expect html files to be at top level of main bundle.
        NSString *bundlePath = [[NSBundle mainBundle] bundlePath];

        // choose one of your html files at random
        NSString *localPath = [[NSString stringWithFormat:@"%@/Myweb%d.html",
            bundlePath, MYRAND(1,20)];

        NSURL *fileURL = [NSURL fileURLWithPath:localPath];
        [self.falView loadRequest: [[NSURLRequest alloc] initWithURL:fileURL]];

}
0

精彩评论

暂无评论...
验证码 换一张
取 消