2010-09-20 103 views
5

我終於得到了我的主要應用程序版本(點擊播放MMO - 檢查出來;-)),我現在正在努力擴展它。使用不規則形狀按鈕的簡單方法

要做到這一點,我需要一個有四個獨立按鈕的圓圈,這些按鈕本質上是四分之一。我得出的結論是,細胞圖像將需要由四個圖像構成,每個四分之一,但由於矩形圖像形狀的必要性,我將以一些重疊結束,儘管重疊將是透明。

讓這個工作的最好方法是什麼?我真的需要一些非常簡單的東西,我看過這個 http://iphonedevelopment.blogspot.com/2010/03/irregularly-shaped-uibuttons.html

之前,但尚未成功地讓它工作。任何人都可以提供一些建議?

如果這有什麼差別,我會被部署到的iOS 3.X框架(將4.2向下行,當4.2出來的iPad)

回答

5

我看不到,爲什麼重疊的需要。
只需創建4個按鈕,併爲每個按鈕分配一張圖片。評論後

編輯

看到這個great project。一個例子正是你想要做的。

它可以通過將在覆蓋

pointInside:withEvent:和UIImage的一個類別,這增加了該方法

- (UIColor *)colorAtPixel:(CGPoint)point { 
    // Cancel if point is outside image coordinates 
    if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, self.size.width, self.size.height), point)) { 
     return nil; 
    } 


    // Create a 1x1 pixel byte array and bitmap context to draw the pixel into. 
    // Reference: http://stackoverflow.com/questions/1042830/retrieving-a-pixel-alpha-value-for-a-uiimage 
    NSInteger pointX = trunc(point.x); 
    NSInteger pointY = trunc(point.y); 
    CGImageRef cgImage = self.CGImage; 
    NSUInteger width = self.size.width; 
    NSUInteger height = self.size.height; 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    int bytesPerPixel = 4; 
    int bytesPerRow = bytesPerPixel * 1; 
    NSUInteger bitsPerComponent = 8; 
    unsigned char pixelData[4] = { 0, 0, 0, 0 }; 
    CGContextRef context = CGBitmapContextCreate(pixelData, 
               1, 
               1, 
               bitsPerComponent, 
               bytesPerRow, 
               colorSpace, 
               kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 
    CGColorSpaceRelease(colorSpace); 
    CGContextSetBlendMode(context, kCGBlendModeCopy); 

    // Draw the pixel we are interested in onto the bitmap context 
    CGContextTranslateCTM(context, -pointX, pointY-(CGFloat)height); 
    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage); 
    CGContextRelease(context); 

    // Convert color values [0..255] to floats [0.0..1.0] 
    CGFloat red = (CGFloat)pixelData[0]/255.0f; 
    CGFloat green = (CGFloat)pixelData[1]/255.0f; 
    CGFloat blue = (CGFloat)pixelData[2]/255.0f; 
    CGFloat alpha = (CGFloat)pixelData[3]/255.0f; 
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha]; 
} 
+0

。我的需要是45度旋轉,所以不幸的是它並不那麼簡單。 – David26th 2010-09-21 11:42:51

+0

現在我看到問題 – vikingosegundo 2010-09-21 12:10:46

+0

@ David26th查看我的編輯 – vikingosegundo 2010-09-21 12:21:48

7

跳過按鈕和簡單地以觸摸響應像素的α值的包含圓的視圖。

爲您想要捕捉觸摸的每個區域創建一個CGPath,當您的UIview收到觸摸時,檢查路徑內的成員資格。

[編輯答案顯示骨架的實施細則 - TomH]

下面我將如何處理這個問題:(我沒有測試過這個代碼和語法可能不是很正確,但是這是總體思路)

1)使用PS或你最喜歡的圖像創建應用程序,創建一個四分之一圓圈的PNG。將它添加到您的XCode項目中。

2)添加一個UIView到用戶界面。將UIView的圖層內容設置爲png。

self.myView = [[UIView alloc] initWithRect:CGRectMake(10.0, 10.0, 100.0, 100,0)]; 
[myView.layer setContents:(id)[UIImage loadImageNamed:@"my.png"]]; 

3)創建CGPaths,描述您感興趣的UIView中的區域。

self.quadrantOnePath = CGPathCreateMutable(); 
CGPathMoveToPoint(self.quadrantOnePath, NULL, 50.0, 50.0); 
CGPathAddLineToPoint(self.quadrantOnePath, NULL, 100.0, 50.0); 
CGPathAddArc(self.quadrantOnePath, NULL, 50.0, 50.0, 50.0, 0.0, M_PI2, 1); 
CGPathCloseSubpath(self.quadrantOnePath); 

// create paths for the other 3 circle quadrants too! 

4)添加UIGestureRecognizer和收聽/視圖

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; 
[tapRecognizer setNumberOfTapsRequired:2]; // default is 1 

5)觀察絲錐當tapRecognizer調用它的目的地選擇

- (void)handleGesture:(UIGestureRecognizer *) recognizer { 

    CGPoint touchPoint = [recognizer locationOfTouch:0 inView:self.myView]; 
    bool processTouch = CGPathContainsPoint(self.quadrantOnePath, NULL, touchPoint, true); 

    if(processTouch) { 
    // call your method to process the touch 
    } 
} 

不要忘記釋放一切適當時 - 使用CGPathRelease釋放路徑。

另一個想法:如果您用來表示圓形象限的圖形只是填充顏色(即沒有花哨的圖形,圖層效果等),那麼您也可以使用在UIView的drawRect方法中創建的路徑畫出象限。這將解決上述方法的缺陷之一:圖形和用於檢查觸摸的路徑之間沒有緊密集成。也就是說,如果您換出不同圖形的圖形,請更改圖形的大小等,則用於檢查觸摸的路徑將不同步。可能是一個高維護代碼。

+0

這將是理想和最不混亂的解決方案 - 您能詳細說明如何做到這一點嗎? – David26th 2010-09-26 18:56:20

+0

編輯上面的答案來說明這樣做的一種方法。 – TomH 2010-09-27 13:08:07

4

這裏是一個真棒項目,解決了不規則形狀的按鈕,這樣容易的問題:如果它是如此簡單 http://christinemorris.com/2011/06/ios-irregular-shaped-buttons/

+1

引用技術的另一個鏈接:[http://oleb.net/blog/2009/10/obshapedbutton-non-rectangular-buttons-on-the-iphone/](http://oleb.net/blog/2009/10/obshapedbutton-non-rectangular-buttons-on-the-iphone /) – 2012-01-05 19:24:54

+0

在鏈接的項目中有非常多的功能,只需很少的努力。真的值得一看! – 2012-03-19 22:27:19

+0

這太棒了!這應該是正確的答案! – 2013-03-17 11:15:28