Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Example/Classes/SAMDemoViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@ - (void)viewDidLoad {

self.title = @"Gradient View";

SAMGradientView *gradientView = [[SAMGradientView alloc] initWithFrame:CGRectMake(20.0f, 20.0f, 280.0f, 280.0f)];
SAMGradientView *gradientView = [[SAMGradientView alloc] initWithFrame:CGRectMake(20.0f, 70.0f, 280.0f, 280.0f)];
gradientView.gradientColors = @[
[UIColor colorWithRed:0.0f green:0.0f blue:1.0f alpha:1.0f],
[UIColor colorWithRed:0.0f green:0.0f blue:0.5f alpha:1.0f]
[UIColor colorWithRed:0.0f green:0.0f blue:0.5f alpha:1.0f],
[UIColor colorWithRed:0.0f green:0.0f blue:1.0f alpha:1.0f]
];
gradientView.gradientLocations = @[@0.0, @0.25, @0.5, @0.75, @1.0];
gradientView.gradientStartPoint = CGPointMake(280, 0);
gradientView.gradientEndPoint = CGPointMake(0, 280);
gradientView.gradientDirection = SAMGradientViewDirectionNone;

[self.view addSubview:gradientView];

#ifdef __IPHONE_7_0
UIButton *showAlertButton = [[UIButton alloc] initWithFrame:CGRectMake(20.0f, 320.0f, 280.0f, 30.0f)];
UIButton *showAlertButton = [[UIButton alloc] initWithFrame:CGRectMake(20.0f, 370.0f, 280.0f, 30.0f)];
[showAlertButton setTitle:@"Show Alert" forState:UIControlStateNormal];
[showAlertButton addTarget:self action:@selector(showAlert:) forControlEvents:UIControlEventTouchUpInside];
showAlertButton.backgroundColor = [UIColor redColor];
Expand Down
15 changes: 14 additions & 1 deletion SAMGradientView/SAMGradientView.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ typedef enum : NSUInteger {
SAMGradientViewDirectionVertical,

/** The gradient is horizontal. */
SAMGradientViewDirectionHorizontal
SAMGradientViewDirectionHorizontal,
/** The gradient is build based on gradientStartPoint and gradientEndPoint. */
SAMGradientViewDirectionNone
} SAMGradientViewDirection;


Expand Down Expand Up @@ -50,6 +52,17 @@ typedef enum : NSUInteger {
@property (nonatomic, copy) NSArray *dimmedGradientColors;
#endif

/**
Starting point of gradient used only if `gradientDirection` is set to `SAMGradientViewDirectionTwoPoints`
*/

@property (nonatomic, assign) CGPoint gradientStartPoint;

/**
Ending point of gradient used only if `gradientDirection` is set to `SAMGradientViewDirectionTwoPoints`
*/
@property (nonatomic, assign) CGPoint gradientEndPoint;

/**
An optional array of `NSNumber` objects defining the location of each gradient stop.

Expand Down
22 changes: 18 additions & 4 deletions SAMGradientView/SAMGradientView.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ @implementation SAMGradientView
@synthesize bottomInsetColor = _bottomInsetColor;
@synthesize leftBorderColor = _leftBorderColor;
@synthesize leftInsetColor = _leftInsetColor;
@synthesize gradientStartPoint = _gradientStartPoint;
@synthesize gradientEndPoint = _gradientEndPoint;

#ifdef __IPHONE_7_0
@synthesize dimmedGradientColors = _dimmedGradientColors;
Expand Down Expand Up @@ -173,11 +175,23 @@ - (void)drawRect:(CGRect)rect {
// Gradient
if (self.gradient) {
CGPoint start = CGPointMake(0.0f, 0.0f);
CGPoint end = (self.gradientDirection == SAMGradientViewDirectionVertical ? CGPointMake(0.0f, size.height) :
CGPointMake(size.width, 0.0f));
CGContextDrawLinearGradient(context, self.gradient, start, end, kNilOptions);
CGPoint end;

switch (self.gradientDirection) {
case SAMGradientViewDirectionVertical:
end = CGPointMake(0.0f, size.height);
CGContextDrawLinearGradient(context, self.gradient, start, end, kNilOptions);
break;
case SAMGradientViewDirectionHorizontal:
end = CGPointMake(size.width, 0.0f);
CGContextDrawLinearGradient(context, self.gradient, start, end, kNilOptions);
break;
case SAMGradientViewDirectionNone:
CGContextDrawLinearGradient(context, self.gradient, self.gradientStartPoint, self.gradientEndPoint, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
break;
}
}

// Top
if (self.topBorderColor) {
// Top border
Expand Down