Tablo görünümü hücrelerim arka planına birkaç yolla bir degrade koymaya çalışıyorum ve hiçbiri işe yaramıyor. Quarz çekirdekli grafik şeyleri ile çizmeye çalışıyorum, arka plan çıkışını IB üzerinde farklı bir görünüme koyup, arkadan görüntü kodu koyar. Hiçbiri arka tarafta görünmüyor, yanlış olanı anlamıyorum. Gerekirse detaylandırırım, teşekkürler!Tablo görünümünde arka plan üzerinde degrade yerleştirme
2
A
cevap
2
this question'da bir kod buldum ve uygulamam ile çalışacak şekilde uyarlandı. Hem düz hem de gruplandırılmış tablolarla harika çalışır.
//
// UAGradientCellBackgroundLayer.h
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@class UAGradientCellBackgroundView;
@interface UAGradientCellBackgroundLayer : CAGradientLayer {
CGFloat *colorComponents;
BOOL _override;
}
@property(nonatomic,assign) BOOL override;
- (void)setColorComponents:(CGFloat *)components;
@end
//
// UAGradientCellBackgroundLayer.m
//
#import "UAGradientCellBackgroundLayer.h"
#import "UAGradientCellBackgroundView.h"
#define TABLE_CELL_BACKGROUND {1, 1, 1, 1, 204/255.0, 204/255.0, 204/255.0, 1} // #FFFFFF and #DDDDDD
@implementation UAGradientCellBackgroundLayer
@synthesize override = _override;
///////////////////////////////////////////////////////////////////////////////////////////////////
- (id)init {
if ((self = [super init])) {
colorComponents = NSZoneMalloc(NSDefaultMallocZone(), 8*sizeof(CGFloat));
CGFloat c[8] = TABLE_CELL_BACKGROUND;
for (int i = 0; i < 8; i++) {
colorComponents[i] = c[i];
}
// self.cornerRadius = 10;
// self.backgroundColor = [UIColor clearColor].CGColor;
}
return self;
}
- (void) dealloc {
NSZoneFree(NSDefaultMallocZone(), colorComponents);
[super dealloc];
}
- (void)display {
if (_override) {
self.colors =
[NSArray arrayWithObjects:
(id)[UIColor colorWithRed:colorComponents[0] green:colorComponents[1] blue:colorComponents[2] alpha:colorComponents[3]].CGColor,
(id)[UIColor colorWithRed:colorComponents[4] green:colorComponents[5] blue:colorComponents[6] alpha:colorComponents[7]].CGColor,
nil];
} else {
self.colors =
[NSArray arrayWithObjects:
(id)[UIColor clearColor].CGColor,
(id)[UIColor clearColor].CGColor,
nil];
}
[super display];
}
- (void)setColorComponents:(CGFloat *)components {
for (int i = 0; i < 8; i++) {
colorComponents[i] = components[i];
}
}
@end
//
// UAGradientCellBackgroundView.h
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
typedef enum {
UAGradientCellBackgroundViewPositionMiddle = 0,
UAGradientCellBackgroundViewPositionTop,
UAGradientCellBackgroundViewPositionBottom,
UAGradientCellBackgroundViewPositionSingle,
UAGradientCellBackgroundViewPositionPlain
} UAGradientCellBackgroundViewPosition;
@interface UAGradientCellBackgroundView : UIView {
UAGradientCellBackgroundViewPosition position;
CGFloat colors[8];
}
@property(nonatomic, assign) UAGradientCellBackgroundViewPosition position;
- (void)setColors:(CGFloat[8])comps;
@end
//
// UAGradientCellBackgroundView.m
//
#import "UAGradientCellBackgroundView.h"
#import "UAGradientCellBackgroundLayer.h"
#import <QuartzCore/QuartzCore.h>
#define kDefaultMargin 10
#define TABLE_CELL_BACKGROUND {1, 1, 1, 1, 204/255.0, 204/255.0, 204/255.0, 1} // #FFFFFF and #DDDDDD
static void addRoundedRectToPath(CGContextRef context, CGRect rect,
float ovalWidth,float ovalHeight);
@implementation UAGradientCellBackgroundView
@synthesize position;
//////////////////////////////////////////////////////////////////////
// PLAIN CELL
//
// layerClass
//
// returns a CAGradientLayer class as the default layer class for this view
//
+ (Class)layerClass {
return [UAGradientCellBackgroundLayer class];
}
//////////////////////////////////////////////////////////////////////
- (void)updateLayer {
UAGradientCellBackgroundLayer* layer = (UAGradientCellBackgroundLayer*)self.layer;
// This is dramatically faster than calling drawRect.
[layer setColorComponents:colors];
layer.override = (self.position == UAGradientCellBackgroundViewPositionPlain);
[layer setNeedsDisplay];
}
////////////////////////////////////////////////////////////////////
// GROUPED CELL
- (void)setColors:(CGFloat[8])comps {
for (int i = 0; i < 8; i++) {
colors[i] = comps[i];
}
}
- (BOOL)isOpaque {
return YES;
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self != nil) {
CGFloat comps[8] = TABLE_CELL_BACKGROUND;
[self setColors:comps];
}
return self;
}
- (void)debugView:(UIView *)view {
NSLog(@"%@ - %@", view.backgroundColor, [[view class] description]);
for (UIView* child in view.subviews) {
[self debugView:child];
}
}
-(void)drawRect:(CGRect)aRect {
[super drawRect:aRect];
// Drawing code
if (position == UAGradientCellBackgroundViewPositionPlain) {
return;
}
CGContextRef c = UIGraphicsGetCurrentContext();
// TODO - Dirty, Dirty hack to fix the background black corners on pop issue. doesnt handle rotation.
// CGContextSetFillColorWithColor(c, [UAColor offsetPinstripesColor].CGColor);
// CGContextFillRect(c, aRect);
int lineWidth = 1;
CGRect rect = [self bounds];
rect.size.width -= lineWidth;
rect.size.height -= lineWidth;
rect.origin.x += lineWidth/2.0;
rect.origin.y += lineWidth/2.0;
CGFloat minx = CGRectGetMinX(rect), midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect);
CGFloat miny = CGRectGetMinY(rect), midy = CGRectGetMidY(rect), maxy = CGRectGetMaxY(rect);
miny -= 1;
CGFloat locations[2] = { 0.0, 1.0 };
CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef myGradient = nil;
CGContextSetStrokeColorWithColor(c, [UIColor darkGrayColor].CGColor);
CGContextSetLineWidth(c, lineWidth);
CGContextSetAllowsAntialiasing(c, YES);
CGContextSetShouldAntialias(c, YES);
if (position == UAGradientCellBackgroundViewPositionTop) {
miny += 1;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, minx, maxy);
CGPathAddArcToPoint(path, NULL, minx, miny, midx, miny, kDefaultMargin);
CGPathAddArcToPoint(path, NULL, maxx, miny, maxx, maxy, kDefaultMargin);
CGPathAddLineToPoint(path, NULL, maxx, maxy);
CGPathAddLineToPoint(path, NULL, minx, maxy);
CGPathCloseSubpath(path);
// Fill and stroke the path
CGContextSaveGState(c);
CGContextAddPath(c, path);
CGContextClip(c);
myGradient = CGGradientCreateWithColorComponents(myColorspace, colors, locations, 2);
CGContextDrawLinearGradient(c, myGradient, CGPointMake(minx,miny), CGPointMake(minx,maxy), 0);
CGContextAddPath(c, path);
CGPathRelease(path);
CGContextStrokePath(c);
CGContextRestoreGState(c);
} else if (position == UAGradientCellBackgroundViewPositionBottom) {
//maxy -= 1; // -1 for the shadow
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, minx, miny);
CGPathAddArcToPoint(path, NULL, minx, maxy, midx, maxy, kDefaultMargin);
CGPathAddArcToPoint(path, NULL, maxx, maxy, maxx, miny, kDefaultMargin);
CGPathAddLineToPoint(path, NULL, maxx, miny);
CGPathAddLineToPoint(path, NULL, minx, miny);
CGPathCloseSubpath(path);
// Fill and stroke the path
CGContextSaveGState(c);
CGContextAddPath(c, path);
CGContextClip(c);
myGradient = CGGradientCreateWithColorComponents(myColorspace, colors, locations, 2);
CGContextDrawLinearGradient(c, myGradient, CGPointMake(minx,miny), CGPointMake(minx,maxy), 0);
CGContextAddPath(c, path);
CGPathRelease(path);
CGContextStrokePath(c);
CGContextRestoreGState(c);
} else if (position == UAGradientCellBackgroundViewPositionMiddle) {
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, minx, miny);
CGPathAddLineToPoint(path, NULL, maxx, miny);
CGPathAddLineToPoint(path, NULL, maxx, maxy);
CGPathAddLineToPoint(path, NULL, minx, maxy);
CGPathAddLineToPoint(path, NULL, minx, miny);
CGPathCloseSubpath(path);
// Fill and stroke the path
CGContextSaveGState(c);
CGContextAddPath(c, path);
CGContextClip(c);
myGradient = CGGradientCreateWithColorComponents(myColorspace, colors, locations, 2);
CGContextDrawLinearGradient(c, myGradient, CGPointMake(minx,miny), CGPointMake(minx,maxy), 0);
CGContextAddPath(c, path);
CGPathRelease(path);
CGContextStrokePath(c);
CGContextRestoreGState(c);
} else if (position == UAGradientCellBackgroundViewPositionSingle) {
miny += 1;
//maxy -= 1; // -1 for the shadow
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, minx, midy);
CGPathAddArcToPoint(path, NULL, minx, miny, midx, miny, kDefaultMargin);
CGPathAddArcToPoint(path, NULL, maxx, miny, maxx, midy, kDefaultMargin);
CGPathAddArcToPoint(path, NULL, maxx, maxy, midx, maxy, kDefaultMargin);
CGPathAddArcToPoint(path, NULL, minx, maxy, minx, midy, kDefaultMargin);
CGPathCloseSubpath(path);
// Shadow
// CGContextAddPath(c, path);
// CGContextSaveGState(c);
// CGContextSetShadow(c, CGSizeMake(0, -1), 1);
// CGContextFillPath(c);
// Fill and stroke the path
CGContextSaveGState(c);
CGContextAddPath(c, path);
CGContextClip(c);
myGradient = CGGradientCreateWithColorComponents(myColorspace, colors, locations, 2);
CGContextDrawLinearGradient(c, myGradient, CGPointMake(minx,miny), CGPointMake(minx,maxy), 0);
CGContextAddPath(c, path);
CGPathRelease(path);
CGContextStrokePath(c);
CGContextRestoreGState(c);
}
CGColorSpaceRelease(myColorspace);
CGGradientRelease(myGradient);
return;
}
- (void)setPosition:(UAGradientCellBackgroundViewPosition)newPosition {
if (position != newPosition) {
position = newPosition;
[self updateLayer];
}
}
@end
static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth,float ovalHeight) {
float fw, fh;
if (ovalWidth == 0 || ovalHeight == 0) {// 1
CGContextAddRect(context, rect);
return;
}
CGContextSaveGState(context);// 2
CGContextTranslateCTM (context, CGRectGetMinX(rect),// 3
CGRectGetMinY(rect));
CGContextScaleCTM (context, ovalWidth, ovalHeight);// 4
fw = CGRectGetWidth (rect)/ovalWidth;// 5
fh = CGRectGetHeight (rect)/ovalHeight;// 6
CGContextMoveToPoint(context, fw, fh/2); // 7
CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);// 8
CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);// 9
CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);// 10
CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // 11
CGContextClosePath(context);// 12
CGContextRestoreGState(context);// 13
}
Kullanım örneği (düz tablo): Bu arka plan olarak neden çalışmaz
#import "UAGradientCellBackgroundView.h"
CGFloat COLORS[8] = { 0.0/255, 0.0/255, 255.0/255, 1.0, 0.0/255, 0.0/255, 0.0/255, 1.0 }
...
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellID = @"CellID";
// create cell
UITableViewCell *cell = (UITableViewCell *)[table dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
UAGradientCellBackgroundView *bgView = [[[UAGradientCellBackgroundView alloc] initWithFrame:CGRectZero] autorelease];
[bgView setBackgroundColor:[UIColor clearColor]];
[bgView setColors:COLORS];
[bgView setPosition:UAGradientCellBackgroundViewPositionPlain];
cell.selectedBackgroundView = bgView;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.imageView.image = [UIImage imageNamed:@"cellimg.png"];
}
cell.textLabel.text = @"Text";
return cell;
}
İlgili konular
- 1. Çoklu arka plan resimleri (degrade + hareketli)
- 2. CSS Arka Plan Degrade ile ofset
- 3. Özel (degrade) arka plan ActionBar Uyumluluk
- 4. Degrade arka plan rengi açık :: seçim
- 5. Malzeme temalı araç çubuğundaki degrade arka plan
- 6. Tablo satır arka plan görüntüsü
- 7. Arka plan resmi üzerinde css gradyanı kullan
- 8. Style.DataTrigger Sınır arka plan üzerinde
- 9. CSS ile degrade arka plan dolgu sayfası oluşturma
- 10. Instagram uygulaması gibi hareketli arka plan degrade nasıl oluşturulur?
- 11. Arka plan konumu CSS animasyonu ve doğrusal degrade ile çalışmıyor
- 12. Tablo sütununun arka plan rengini değiştirme javascript
- 13. Tablo satırının arka plan rengini ayarlama
- 14. UISearchDisplayController'ın tablo görünümüne nasıl özel bir arka plan eklenir?
- 15. arka plan üzerinde değişiklik yapın onclick sınıf
- 16. UIWebView üzerinde arka plan resmi görüntüleme
- 17. Arka plan resmi üzerinde kenarlık yarıçapı
- 18. AppHarbor üzerinde arka plan görevini simüle etme
- 19. textview arka plan rengi üzerinde popupwindow değiştirmiyor
- 20. Python'da bir görüntü arka plan üzerinde çizim
- 21. CSS3 animasyon arka plan ateş üzerinde çalışmaz
- 22. iOS: Tablo görünümünde UISearchbar ekleme Tablo görünümünde seçenekler
- 23. Arka plan görüntüsü üzerinde şeffaf bir css3 gradyanını kaplayabilir misiniz?
- 24. iOS'ta UITableViewCell'de degrade arka planı nasıl yapılır?
- 25. iOS 6 arka plan resmi
- 26. Android: Kullanıcı liste görünümünde öğeyi seçtiğinde arka plan rengini değiştirme.
- 27. ggplot ile arka plan resmi üzerinde veri çizimi
- 28. Radyal degrade arka planı nasıl konumlandırılır
- 29. UISearchBar üzerindeki UITableView arka plan rengi
- 30. Tablo arka plan rengi, renkli kenarlık yanılsaması verir
Ben arka plan sadece hücre seperatörler koyu yapar görüntülemek olarak ayarlamak çalışırsanız, ben alamadım görünüm, ancak seçilen görünüm olarak çalışır? –
Umm ... 'selectedBackgroundView' ifadesini 'backgroundView' olarak değiştirmek benim için iyi çalışıyor, muhtemelen kodunuzda başka bir sorun var ya da yapıştırılan kodda başka değişiklikler yaptınız. Belki hem arka plan rengini hem de hücrelerin arka plan görünümünü ayarlıyorsunuzdur? Ya da farklı gradyanlara sahip çeşitli hücre tipleriniz var ve farklı hücre kimlikleri kullanmıyor musunuz? - bunlar bir noktada karşılaştığım iki problem. – cristis